*******************************************
*******************************************
*			Plugged versus Unplugged	  *
*			   Balance Table			  *
*				Pool Residual Comparisons *
*******************************************
*******************************************
{	
	clear
	set more off
	clear mata
	clear matrix
	set maxvar 10000
	cd "C:\Users\maxha\Box\EDWIN_DEP_Well_Data\Washington_County_Project\Final Code\Compiling Near Table\" 	/*Home PC*/ 
	use WC_Wells_Soil_Near_Withdata_Fields_Pools.dta

*END YEAR MACRO

	/*TURNS ON 2005 END YEAR*/
		*global endyear = "2005"
	/*TURNS ON 2017 END YEAR*/
		global endyear = "2017"
	/*CHANGE TO MATCH THE MACRO*/
		scalar end = 2017

***GENERATE VARIABLES***

*WELL UNPLUGGED
	replace plugged = 2 if plugged == 1
	replace plugged = 1 if plugged == 0
	replace plugged = 0 if plugged == 2
	rename plugged unplugged
	
*UN/PLUGGED OVER PERIOD
	replace date_plugg_year=1 if unplugged==0 & date_plugg_year==.
	gen bplugged1 = (date_plugg_year<1970 & unplugged==0) /*1= plugged for entire period*/
	gen bunplugged2005 = ((date_plugg_year>2005 & unplugged==0)|unplugged==1) /*1= unplugged for entire period*/
	gen bunplugged2017 = ((date_plugg_year>2017 & unplugged==0)|unplugged==1) /*1= unplugged for entire period*/

*WELL TYPE BINARIES
	gen oil_type= (welltype=="OIL")
	gen gas_type= (welltype=="GAS")
	gen oil2_type= (welltype=="OIL AND GAS")
	
*SOIL TYPE BINARIES
	gen soil_WB_notlim= (dwelling_with_basement=="Not limited")
	gen soil_WB_somlim= (dwelling_with_basement=="Somewhat limited")
	gen soil_WB_vrylim= (dwelling_with_basement=="Very limited")
	gen soil_WOB_notlim= (dwelling_without_basement=="Not limited")
	gen soil_WOB_somlim= (dwelling_without_basement=="Somewhat limited")
	gen soil_WOB_vrylim= (dwelling_without_basement=="Very limited") /*"Not rated" omitted for both*/
	
*NEAREST
	egen Nearest_Park_Gameland= rowmin(Nearest_Pa Nearest_Ga)

*SAMPLE CONSTRUCTION
	drop if minyear>=1970 /*Only including Pre-1970 Wells*/
	replace bplugged1=0 if (bplugged1==1 & last_produ_report_year>=1970 & last_produ_report_year!=.) | (bplugged1==1 & first_produ_report_year>=1970 & first_produ_report_year!=.)| (bplugged1==1 & wellstatus=="DEP PLUGGED")  /*29 wells without plugging dates were likely plugged recently and must be dropped in the next line*/
	keep if bplugged1==1 | bunplugged$endyear ==1 /*Keeping only wells plugged for the entire period, or not plugged for the entire period. Getting rid of "switchers"*/
	drop if Nearest_Ci<=300 /*Dropping those observations in or within 300 m from Washington or Canonsburg*/
	keep if oil_type==1 | gas_type==1 | oil2_type==1 /*Keeping only oil and gas wells*/


*#1: Covariates that Affect Building*

preserve
	xi: regress Nearest_79 i.Pool_num
	predict yhat
	gen uhat= Nearest_79-yhat
	ttest uhat, by(bplugged1) unequal
	matrix N1= r(N_1)
	matrix N2= r(N_2)
	matrix N3= .
	matrix N4= .
	matrix a1= r(mu_1)
	matrix a2= r(mu_2)
	matrix a4= r(p)
	stddiff uhat, by(bplugged1)
	matrix a3= r(stddiff)
	matrix q1= a1,a2,a3,a4
	matrix rownames q1 = "Distance to I-79"
	matrix colnames q1 = "Unplugged (N=1507)" "Plugged (N=2467)" "Standardized Mean Difference" "P-Value"
	matrix q=N1,N2,N3,N4
	matrix rownames q = "Sample Size"
restore

preserve
	xi: regress Nearest_Wa i.Pool_num
	predict yhat
	gen uhat= Nearest_Wa-yhat
	ttest uhat, by(bplugged1) unequal
	matrix a1= r(mu_1)
	matrix a2= r(mu_2)
	matrix a4= r(p)
	stddiff uhat, by(bplugged1)
	matrix a3= r(stddiff)
	matrix q2= a1,a2,a3,a4
	matrix rownames q2 = "Distance to Water "
restore

preserve
	xi: regress Nearest_Co i.Pool_num
	predict yhat
	gen uhat= Nearest_Co-yhat
	ttest uhat, by(bplugged1) unequal
	matrix a1= r(mu_1)
	matrix a2= r(mu_2)
	matrix a4= r(p)
	stddiff uhat, by(bplugged1)
	matrix a3= r(stddiff)
	matrix q3= a1,a2,a3,a4
	matrix rownames q3 = "Distance to Coal Mine"
restore

preserve
	xi: regress Nearest_Park_Gameland i.Pool_num
	predict yhat
	gen uhat= Nearest_Park_Gameland-yhat
	ttest uhat, by(bplugged1) unequal
	matrix a1= r(mu_1)
	matrix a2= r(mu_2)
	matrix a4= r(p)
	stddiff uhat, by(bplugged1)
	matrix a3= r(stddiff)
	matrix q4= a1,a2,a3,a4
	matrix rownames q4 = "Distance to Park or Gameland"
restore

preserve
	xi: regress soil_WB_vrylim i.Pool_num
	predict yhat
	gen uhat= soil_WB_vrylim-yhat
	ttest uhat, by(bplugged1) unequal
	matrix a1= r(mu_1)
	matrix a2= r(mu_2)
	matrix a4= r(p)
	stddiff uhat, by(bplugged1)
	matrix a3= r(stddiff)
	matrix q5= a1,a2,a3,a4
	matrix rownames q5 = "Soil Very Limited (Basement)"
restore

preserve
	xi: regress soil_WOB_vrylim i.Pool_num
	predict yhat
	gen uhat= soil_WOB_vrylim-yhat
	ttest uhat, by(bplugged1) unequal
	matrix a1= r(mu_1)
	matrix a2= r(mu_2)
	matrix a4= r(p)
	stddiff uhat, by(bplugged1)
	matrix a3= r(stddiff)
	matrix q6= a1,a2,a3,a4
	matrix rownames q6 = "Soil Very Limited (No Basement)"
restore

preserve
	xi: regress slope i.Pool_num
	predict yhat
	gen uhat= slope-yhat
	ttest uhat, by(bplugged1) unequal
	matrix a1= r(mu_1)
	matrix a2= r(mu_2)
	matrix a4= r(p)
	stddiff uhat, by(bplugged1)
	matrix a3= r(stddiff)
	matrix q7= a1,a2,a3,a4
	matrix rownames q7 = "Slope"
restore

matrix t1=q1\q2\q3\q4\q5\q6\q7
	
esttab matrix(t1, fmt(2)) using "C:\Users\maxha\Box\Abandoned Wells and Building\JAERE\Final Submission Files\p_up_balance.tex",  nomtitles replace	
	}
*******************************************
*******************************************
*			Unconditional				  *
*			   Building 				  *
*				Figures					  *
*******************************************
*******************************************	
{
	clear
	clear matrix
	clear mata
	set maxvar 10000
	set more off
	*cd "C:\Users\mrh105\Box\EDWIN_DEP_Well_Data\Washington_County_Project\Circle and Doughnut Tables" /*PC*/
	cd "C:\Users\maxha\Box\EDWIN_DEP_Well_Data\Washington_County_Project\Final Code\Circle and Doughnut Tables" /*Home PC*/

	*END YEAR MACRO

		/*TURNS ON 2005 END YEAR*/
			*global endyear = "2005"
		/*TURNS ON 2017 END YEAR*/
			global endyear = "2017"
		/*TURNS ON UNLIMITED START YEAR */
			global startyear = "0"
		/*TURNS ON 1920 START YEAR*/
			*global startyear = "1920"

	*CIRCLE/DOUGHNUT/NEAR_FAR MACROS
		
		*Turns on doughnut globals, must also turn on doughnut dependent variables and turn off either circle table or near_far table
		global d_c= "doughnut_"
		global low_high1 = "0_50"
		global low_high2 = "_50_100"
		global low_high3 = "100_150"
		global low_high4 = "150_200"
		global low_high5 = "200_250"
		
	use Table_$d_c$low_high1$low_high2.dta
		
	*SAMPLE CONSTRUCTION
		drop if minyear>=1970 /*Only including Pre-1970 Wells in the regressions*/
		replace bplugged1=0 if (bplugged1==1 & last_produ_report_year>=1970 & last_produ_report_year!=.) | (bplugged1==1 & first_produ_report_year>=1970 & first_produ_report_year!=.)| (bplugged1==1 & wellstatus=="DEP PLUGGED")  /*29 wells without plugging dates were likely plugged recently and must be dropped in the next line*/
		keep if bplugged1==1 | bunplugged$endyear ==1 /*Keeping only wells plugged for the entire period, or not plugged for the entire period. Getting rid of "switchers"*/
		drop if Nearest_Ci<=300 /*Dropping those observations in or within 300 m from Washington or Canonsburg*/
		keep if oil_type==1 | gas_type==1 | oil2_type==1 /*Keeping only oil, gas, and oil and gas wells*/

	*TREATMENT VARIABLE
		rename bunplugged$endyear unplugged_treat
		tab unplugged_treat /*1's are unplugged for the whole period, zeros are plugged for the whole period*/

	*DOUGHNUT DEPENDENT VARIABLES
		foreach n in 2005 2017  {
			gen post_building_`n'=(post_build_`n'>0)
				replace post_building_`n'= (post_building_`n')*(((50^2)*_pi)/(((100^2)*_pi)-((50^2)*_pi))) if $d_c$low_high2 ==1
				replace post_building_`n'= (post_building_`n')*(((50^2)*_pi)/(((150^2)*_pi)-((100^2)*_pi))) if $d_c$low_high3 ==1
				replace post_building_`n'= (post_building_`n')*(((50^2)*_pi)/(((200^2)*_pi)-((150^2)*_pi))) if $d_c$low_high4 ==1
				replace post_building_`n'= (post_building_`n')*(((50^2)*_pi)/(((250^2)*_pi)-((200^2)*_pi))) if $d_c$low_high5 ==1
			gen post_SqM_Per_100SqM_`n'= (post_SqM_`n')/((50^2)*_pi*(1/100))
				replace post_SqM_Per_100SqM_`n'= (post_SqM_`n')/((((100^2)*_pi)-((50^2)*_pi))*(1/100)) if $d_c$low_high2 ==1
				replace post_SqM_Per_100SqM_`n'= (post_SqM_`n')/((((150^2)*_pi)-((100^2)*_pi))*(1/100)) if $d_c$low_high3 ==1
				replace post_SqM_Per_100SqM_`n'= (post_SqM_`n')/((((200^2)*_pi)-((150^2)*_pi))*(1/100)) if $d_c$low_high4 ==1
				replace post_SqM_Per_100SqM_`n'= (post_SqM_`n')/((((250^2)*_pi)-((200^2)*_pi))*(1/100)) if $d_c$low_high5 ==1
			}
			
		foreach s in 0 1920 {
			gen pre_building_`s'=(pre_build_y`s'>0)
				replace pre_building_`s'= (pre_building_`s')*(((50^2)*_pi)/(((100^2)*_pi)-((50^2)*_pi))) if $d_c$low_high2 ==1
				replace pre_building_`s'= (pre_building_`s')*(((50^2)*_pi)/(((150^2)*_pi)-((100^2)*_pi))) if $d_c$low_high3 ==1
				replace pre_building_`s'= (pre_building_`s')*(((50^2)*_pi)/(((200^2)*_pi)-((150^2)*_pi))) if $d_c$low_high4 ==1
				replace pre_building_`s'= (pre_building_`s')*(((50^2)*_pi)/(((250^2)*_pi)-((200^2)*_pi))) if $d_c$low_high5 ==1
				
			gen pre_SqM_Per_100SqM_`s'= (pre_SqM_y`s')/((50^2)*_pi*(1/100))
				replace pre_SqM_Per_100SqM_`s'= (pre_SqM_y`s')/((((100^2)*_pi)-((50^2)*_pi))*(1/100)) if $d_c$low_high2 ==1
				replace pre_SqM_Per_100SqM_`s'= (pre_SqM_y`s')/((((150^2)*_pi)-((100^2)*_pi))*(1/100)) if $d_c$low_high3 ==1
				replace pre_SqM_Per_100SqM_`s'= (pre_SqM_y`s')/((((200^2)*_pi)-((150^2)*_pi))*(1/100)) if $d_c$low_high4 ==1
				replace pre_SqM_Per_100SqM_`s'= (pre_SqM_y`s')/((((250^2)*_pi)-((200^2)*_pi))*(1/100)) if $d_c$low_high5 ==1
				
				gen pre_SqM_Per_100SqM_`s'_2= pre_SqM_Per_100SqM_`s'^2
			}
			
			gen pre_SqM_Per_100SqM_0_to_1920= pre_SqM_Per_100SqM_0-pre_SqM_Per_100SqM_1920
			gen building_0_1920=(pre_SqM_Per_100SqM_0_to_1920>0)
				replace building_0_1920= (building_0_1920)*(((50^2)*_pi)/(((100^2)*_pi)-((50^2)*_pi))) if $d_c$low_high2 ==1
				replace building_0_1920= (building_0_1920)*(((50^2)*_pi)/(((150^2)*_pi)-((100^2)*_pi))) if $d_c$low_high3 ==1
				replace building_0_1920= (building_0_1920)*(((50^2)*_pi)/(((200^2)*_pi)-((150^2)*_pi))) if $d_c$low_high4 ==1
				replace building_0_1920= (building_0_1920)*(((50^2)*_pi)/(((250^2)*_pi)-((200^2)*_pi))) if $d_c$low_high5 ==1
			gen new_y_build= post_building_$endyear - building_0_1920
			gen new_y_sqm= post_SqM_Per_100SqM_$endyear - pre_SqM_Per_100SqM_0_to_1920
			
	*BUILDING BINARY FIGURE:
	
	*x axis
	
	matrix doughnuts=1\2\3\4\5

	*average plugged
	qui: mean new_y_build if unplugged_treat==0, over(bldg_doughnut_) 
	matrix a1=e(b)'
	
	*average unplugged
	qui: mean  new_y_build if unplugged_treat==1, over(bldg_doughnut_)
	matrix a2=e(b)'
	
	*graphing
	matrix a= a1,a2, doughnuts
	svmat a, names(a)
	
	label define dlabel 1 "0 to 50m" 2 "50 to 100m" 3 "100 to 150m" 4 "150 to 200m" 5 "200 to 250m"
	label values a3 dlabel
	
	format  %9.3f a1 a2
	
	qui: scatter a1 a2 a3, ytitle("Probability of Building", margin(medium) size(large) ) ylab(0 (.05).25, labsize(medium) nogrid format(%9.0gc)) ysize(3.5)  /*
	*/ legend(label(1 "Plugged") label(2 "Unplugged")) /*
	*/ xtitle("Location", margin(medium) size(large)) xlab(1(1)5, valuelabel labsize(medium)) xsize(6) graphregion(lcolor(white) fcolor(white)) bgcolor(white) plotregion(lstyle(none) fcolor(none)) /*
	*/ lcolor(black black) lwidth(thick thick) lpattern(solid longdash) /*
	*/ graphregion(margin(2 10 2 2)) /*
	*/ mlabel(a1 a2) msymbol(T S) connect(1 1) /*
	*/  mlabposition(1 5) mlabcolor(gs0 gs1)  mlabgap(2 2) mcolor(gs0 gs1)
	graph export "C:\Users\maxha\Box\Abandoned Wells and Building\JAERE\Final Submission Files\uncond_binary.jpg", width(4800) as(jpg) replace
	
	*BUILDING BINARY DiD FIGURE:
	
	bysort well_id (a3): gen a4= a1[_N]
	bysort well_id (a3): gen a5= a2[_N]
	
	gen a6=(a2-a1)-(a5-a4) if a3==1
	replace a6=(a2-a1)-(a5-a4) if a3==2
	replace a6=(a2-a1)-(a5-a4) if a3==3
	replace a6=(a2-a1)-(a5-a4) if a3==4
	
	format  %9.3f a6

		
	qui: scatter a6 a3, ytitle("Probability of Building", margin(medium) size(large) ) ylab(-.1 (.05)0, labsize(medium) nogrid format(%9.0gc)) ysize(3.5)  /*
	*/ xtitle("Location", margin(medium) size(large)) xlab(1(1)4, valuelabel labsize(medium)) xsize(6) graphregion(lcolor(white) fcolor(white)) bgcolor(white) plotregion(lstyle(none) fcolor(none))/*
	*/ lcolor(black) lwidth(thick) lpattern(solid) /*
	*/ graphregion(margin(2 2 2 2)) /*
	*/ mlabel(a6) msymbol(S) connect(1) /*
	*/  mlabposition(5) mlabcolor(gs0)  mlabgap(2) mcolor(gs0)
	graph export "C:\Users\maxha\Box\Abandoned Wells and Building\JAERE\Final Submission Files\did_binary.jpg", width(4800) as(jpg) replace
	
	
	*SQM FOOTPRINT FIGURE:
	
	*average plugged
	qui: mean  new_y_sqm if unplugged_treat==0, over(bldg_doughnut_) 
	matrix b1=e(b)'
	
	*average unplugged
	qui: mean  new_y_sqm if unplugged_treat==1, over(bldg_doughnut_)
	matrix b2=e(b)'
	
	*graphing
	matrix b= b1,b2, doughnuts
	svmat b, names(b)

	label values b3 dlabel
	
	format  %9.3f b1 b2
	
	scatter b1 b2 b3, ytitle("Building Footprint (SqM Per 100 SqM)", margin(medium) size(large) ) ylab(0 (.5)2.5, labsize(medium) nogrid format(%9.0gc)) ysize(3.5)  /*
	*/ legend(label(1 "Plugged") label(2 "Unplugged")) /*
	*/ xtitle("Location", margin(medium) size(large)) xlab(1(1)5, valuelabel labsize(medium)) xsize(6) graphregion(lcolor(white) fcolor(white)) bgcolor(white) plotregion(lstyle(none) fcolor(none)) /*
	*/ lcolor(black black) lwidth(thick thick) lpattern(solid longdash) /*
	*/ graphregion(margin(2 10 2 12)) /*
	*/ mlabel(b1 b2) msymbol(T S) connect(1 1) /*
	*/  mlabposition(1 5) mlabcolor(gs0 gs1)  mlabgap(2 2) mcolor(gs0 gs1)
	graph export "C:\Users\maxha\Box\Abandoned Wells and Building\JAERE\Final Submission Files\uncond_ftprnt.jpg", width(4800) as(jpg) replace
	
	*SQM FOOTPRINT DiD FIGURE:
	
	bysort well_id (b3): gen b4= b1[_N]
	bysort well_id (b3): gen b5= b2[_N]
	
	gen b6=(b2-b1)-(b5-b4) if b3==1
	replace b6=(b2-b1)-(b5-b4) if b3==2
	replace b6=(b2-b1)-(b5-b4) if b3==3
	replace b6=(b2-b1)-(b5-b4) if b3==4
	
	format  %9.3f b6

		
	qui: scatter b6 b3, ytitle("Building Footprint (SqM Per 100 SqM)", margin(medium) size(large) ) ylab(-1 (.5)0, labsize(medium) nogrid format(%9.0gc)) ysize(3.5)  /*
	*/ xtitle("Location", margin(medium) size(large)) xlab(1(1)4, valuelabel labsize(medium)) xsize(6) graphregion(lcolor(white) fcolor(white)) bgcolor(white) plotregion(lstyle(none) fcolor(none))/*
	*/ lcolor(black) lwidth(thick) lpattern(solid) /*
	*/ graphregion(margin(2 2 2 5)) /*
	*/ mlabel(b6) msymbol(S) connect(1) /*
	*/  mlabposition(5) mlabcolor(gs0)  mlabgap(2) mcolor(gs0)
	graph export "C:\Users\maxha\Box\Abandoned Wells and Building\JAERE\Final Submission Files\did_ftprnt.jpg", width(4800) as(jpg) replace
	
}	
*******************************************
*******************************************
*			MAIN ANALYSIS				  *
*			   0 to 50 as Near			  *
*			   50 to 250 as Far			  *
*******************************************
*******************************************
**#
{
	clear
	clear matrix
	clear mata
	set maxvar 10000
	set more off
	*cd "C:\Users\mrh105\Box\EDWIN_DEP_Well_Data\Washington_County_Project\Circle and Doughnut Tables" /*PC*/
	cd "C:\Users\maxha\Box\EDWIN_DEP_Well_Data\Washington_County_Project\Final Code\Circle and Doughnut Tables" /*Home PC*/

	*END YEAR MACRO

		/*TURNS ON 2005 END YEAR*/
			*global endyear = "2005"
		/*TURNS ON 2017 END YEAR*/
			global endyear = "2017"
		/*TURNS ON UNLIMITED START YEAR */
			global startyear = "0"
		/*TURNS ON 1920 START YEAR*/
			*global startyear = "1920"

	*CIRCLE/DOUGHNUT/NEAR_FAR MACROS
		
		*Turns on near_far globals, must also turn on near_far dependent variables (below) and turn off either circle table or doughnut table
		global d_c= "near_"
		global low_high1 = "0_50"
		global low_high2 = "_50_250"
		global low_high3 = "invalid_1"
		global low_high4 = "invalid_2"
		global low_high5 = "invalid_3"
		
	use Table_$d_c$low_high1$low_high2.dta
		
	*SAMPLE CONSTRUCTION
		drop if minyear>=1970 /*Only including Pre-1970 Wells in the regressions*/
		replace bplugged1=0 if (bplugged1==1 & last_produ_report_year>=1970 & last_produ_report_year!=.) | (bplugged1==1 & first_produ_report_year>=1970 & first_produ_report_year!=.)| (bplugged1==1 & wellstatus=="DEP PLUGGED")  /*29 wells without plugging dates were likely plugged recently and must be dropped in the next line*/
		keep if bplugged1==1 | bunplugged$endyear ==1 /*Keeping only wells plugged for the entire period, or not plugged for the entire period. Getting rid of "switchers"*/
		drop if Nearest_Ci<=300 /*Dropping those observations in or within 300 m from Washington or Canonsburg*/
		keep if oil_type==1 | gas_type==1 | oil2_type==1 /*Keeping only oil, gas, and oil and gas wells*/
		
		
	*TREATMENT VARIABLE
		rename bunplugged$endyear unplugged_treat
		tab unplugged_treat /*1's are unplugged for the whole period, zeros are plugged for the whole period*/

	*NEAR_FAR DEPENDENT VARIABLES

		foreach n in 2005 2017  {
			gen post_building_`n'=(post_build_`n'>0)
				replace post_building_`n'= (post_building_`n')*(((50^2)*_pi)/(((250^2)*_pi)-((50^2)*_pi))) if $d_c$low_high2 ==1
			
			gen post_SqM_Per_100SqM_`n'= (post_SqM_`n')/(((50^2)*_pi)*(1/100))
				replace post_SqM_Per_100SqM_`n'= (post_SqM_`n')/((((250^2)*_pi)-((50^2)*_pi))*(1/100)) if $d_c$low_high2 ==1	

			gen post_com_build_Per_100SqM_`n' =(post_com_build_`n')/(((50^2)*_pi)*(1/100))
				replace post_com_build_Per_100SqM_`n'= (post_com_build_`n')/((((250^2)*_pi)-((50^2)*_pi))*(1/100)) if $d_c$low_high2 ==1
			gen post_res_build_Per_100SqM_`n' =(post_res_build_`n')/(((50^2)*_pi)*(1/100))
				replace post_res_build_Per_100SqM_`n'= (post_res_build_`n')/((((250^2)*_pi)-((50^2)*_pi))*(1/100)) if $d_c$low_high2 ==1
			gen post_out_build_Per_100SqM_`n' =(post_out_build_`n')/(((50^2)*_pi)*(1/100))
				replace post_out_build_Per_100SqM_`n'= (post_out_build_`n')/((((250^2)*_pi)-((50^2)*_pi))*(1/100)) if $d_c$low_high2 ==1
			gen post_RMTOT_Per_100SqM_`n' =(post_RMTOT_`n')/(((50^2)*_pi)*(1/100))
				replace post_RMTOT_Per_100SqM_`n'= (post_RMTOT_`n')/((((250^2)*_pi)-((50^2)*_pi))*(1/100)) if $d_c$low_high2 ==1
			gen post_res_SqM_Per_100SqM_`n'= (post_res_SqM_`n')/(((50^2)*_pi)*(1/100))
				replace post_res_SqM_Per_100SqM_`n'= (post_res_SqM_`n')/((((250^2)*_pi)-((50^2)*_pi))*(1/100)) if $d_c$low_high2 ==1
			gen post_com_SqM_Per_100SqM_`n'= (post_com_SqM_`n')/(((50^2)*_pi)*(1/100))
				replace post_com_SqM_Per_100SqM_`n'= (post_com_SqM_`n')/((((250^2)*_pi)-((50^2)*_pi))*(1/100)) if $d_c$low_high2 ==1
			gen post_out_SqM_Per_100SqM_`n'= (post_out_SqM_`n')/(((50^2)*_pi)*(1/100))
				replace post_out_SqM_Per_100SqM_`n'= (post_out_SqM_`n')/((((250^2)*_pi)-((50^2)*_pi))*(1/100)) if $d_c$low_high2 ==1
			gen post_SFLA_SqM_Per_100SqM_`n'= (post_SFLA_`n')/(((50^2)*_pi)*(1/100))
				replace post_SFLA_SqM_Per_100SqM_`n'= (post_SFLA_`n')/((((250^2)*_pi)-((50^2)*_pi))*(1/100)) if $d_c$low_high2 ==1
				
				gen building_res_1970_`n'= (post_res_SqM_Per_100SqM_`n'>0)
					replace building_res_1970_`n'= (building_res_1970_`n')*(((50^2)*_pi)/(((250^2)*_pi)-((50^2)*_pi))) if $d_c$low_high2 ==1
				gen building_com_1970_`n'= (post_com_SqM_Per_100SqM_`n'>0)
					replace building_com_1970_`n'= (building_com_1970_`n')*(((50^2)*_pi)/(((250^2)*_pi)-((50^2)*_pi))) if $d_c$low_high2 ==1
				gen building_out_1970_`n'= (post_out_SqM_Per_100SqM_`n'>0)
					replace building_out_1970_`n'= (building_out_1970_`n')*(((50^2)*_pi)/(((250^2)*_pi)-((50^2)*_pi))) if $d_c$low_high2 ==1

			}
			
		foreach s in 0 1920 {
			gen pre_building_`s'=(pre_build_y`s'>0)
				replace pre_building_`s'= (pre_building_`s')*(((50^2)*_pi)/(((250^2)*_pi)-((50^2)*_pi))) if $d_c$low_high2 ==1
				
			gen pre_SqM_Per_100SqM_`s'= (pre_SqM_y`s')/(((50^2)*_pi)*(1/100))
				replace pre_SqM_Per_100SqM_`s'= (pre_SqM_y`s')/((((250^2)*_pi)-((50^2)*_pi))*(1/100)) if $d_c$low_high2 ==1
				
				gen pre_SqM_Per_100SqM_`s'_2=pre_SqM_Per_100SqM_`s'^2
			
			
			gen pre_com_build_Per_100SqM_`s' =(pre_com_build_y`s')/(((50^2)*_pi)*(1/100))
				replace pre_com_build_Per_100SqM_`s'= (pre_com_build_y`s')/((((250^2)*_pi)-((50^2)*_pi))*(1/100)) if $d_c$low_high2 ==1
			gen pre_res_build_Per_100SqM_`s' =(pre_res_build_y`s')/(((50^2)*_pi)*(1/100))
				replace pre_res_build_Per_100SqM_`s'= (pre_res_build_y`s')/((((250^2)*_pi)-((50^2)*_pi))*(1/100)) if $d_c$low_high2 ==1
			gen pre_out_build_Per_100SqM_`s' =(pre_out_build_y`s')/(((50^2)*_pi)*(1/100))
				replace pre_out_build_Per_100SqM_`s'= (pre_out_build_y`s')/((((250^2)*_pi)-((50^2)*_pi))*(1/100)) if $d_c$low_high2 ==1
			gen pre_RMTOT_Per_100SqM_`s'= (pre_RMTOT_y`s')/(((50^2)*_pi)*(1/100))
				replace pre_RMTOT_Per_100SqM_`s'= (pre_RMTOT_y`s')/((((250^2)*_pi)-((50^2)*_pi))*(1/100)) if $d_c$low_high2 ==1
			gen pre_res_SqM_Per_100SqM_`s'= (pre_res_SqM_y`s')/(((50^2)*_pi)*(1/100))
				replace pre_res_SqM_Per_100SqM_`s'= (pre_res_SqM_y`s')/((((250^2)*_pi)-((50^2)*_pi))*(1/100)) if $d_c$low_high2 ==1
			gen pre_com_SqM_Per_100SqM_`s'= (pre_com_SqM_y`s')/(((50^2)*_pi)*(1/100))
				replace pre_com_SqM_Per_100SqM_`s'= (pre_com_SqM_y`s')/((((250^2)*_pi)-((50^2)*_pi))*(1/100)) if $d_c$low_high2 ==1
			gen pre_out_SqM_Per_100SqM_`s'= (pre_out_SqM_y`s')/(((50^2)*_pi)*(1/100))
				replace pre_out_SqM_Per_100SqM_`s'= (pre_out_SqM_y`s')/((((250^2)*_pi)-((50^2)*_pi))*(1/100)) if $d_c$low_high2 ==1
			gen pre_SFLA_SqM_Per_100SqM_`s'= (pre_SFLA_y`s')/(((50^2)*_pi)*(1/100))
				replace pre_SFLA_SqM_Per_100SqM_`s'= (pre_SFLA_y`s')/((((250^2)*_pi)-((50^2)*_pi))*(1/100)) if $d_c$low_high2 ==1
			}		
				
	*INTERACTION TERMS
		gen UP_$d_c$low_high1 = $d_c$low_high1 * unplugged_treat
		gen UP_$d_c$low_high2 = $d_c$low_high2 * unplugged_treat
		gen UP_$d_c$low_high3 = $d_c$low_high3 * unplugged_treat
		gen UP_$d_c$low_high4 = $d_c$low_high4 * unplugged_treat
		gen UP_$d_c$low_high5 = $d_c$low_high5 * unplugged_treat
		
	*DROPPING USELESS RINGS
		drop if near_invalid_1==1
		drop if near_invalid_2==1
		drop if near_invalid_3==1
		
	*MACROS
		global landchars slope soil_WB_vrylim
		global nearest Nearest_Wa Nearest_79 Nearest_Park_Gameland Nearest_Co
		global nearby_endyear unplug_pre_well_win_250_0 unplug_post_well_win_250_$endyear plug_pre_well_win_250_0 plug_post_well_win_250_$endyear
		global pre_build pre_building_$startyear pre_SqM_Per_100SqM_$startyear pre_SqM_Per_100SqM_0_2
		*global controls $landchars $nearest $nearby_endyear $pre_build
		global controls $landchars $nearest $nearby_endyear
		
		preserve
			keep if $d_c$low_high1==1
			keep well_id minyear unplugged_treat
			rename well_id NEAR_FID
			save NEAR_fid.dta, replace
		restore
		preserve
			import dbase using "C:\Users\maxha\Box\EDWIN_DEP_Well_Data\Washington_County_Project\Final Code\Circle and Doughnut Tables\Wells_Win_Wells_Near_Table.dbf", clear
			rename IN_FID well_id
			merge m:1 NEAR_FID using NEAR_fid.dta, keep (3)
			gen other_conv_well_250_300=(NEAR_DIST>250 & NEAR_DIST<300)
			gen other_conv_well_0_50=(NEAR_DIST<50)
			gen other_conv_well_0_100=(NEAR_DIST<100)
			gen plug_wells_50_250=(NEAR_DIST>50 & NEAR_DIST<250 & unplugged_treat==0)
			gen unplug_wells_50_250=(NEAR_DIST>50 & NEAR_DIST<250 & unplugged_treat==1)
			collapse (max) other_conv_well_250_300 other_conv_well_0_50 other_conv_well_0_100 (sum) plug_wells_50_250 unplug_wells_50_250, by(well_id)
			save wells_in_wells.dta, replace
		restore
		
		merge m:1 well_id using wells_in_wells.dta, keep (1 3) nogen
		erase wells_in_wells.dta
		erase NEAR_fid.dta
		replace other_conv_well_250_300=0 if other_conv_well_250_300==.
		replace other_conv_well_0_50=0 if other_conv_well_0_50==.
		replace other_conv_well_0_100=0 if other_conv_well_0_100==.
		replace plug_wells_50_250=0 if plug_wells_50_250==.
		replace unplug_wells_50_250=0 if unplug_wells_50_250==.

	}
	preserve
		keep well_id
		duplicates drop well_id, force
		save merge_to_bldg_sqm.dta, replace /*FOR USE IN DO FILE Analysis_SQM_By_Type.do to select only those buildings near wells in the sample*/
	restore
	******************************
	******************************
			*DiD TABLES*
	******************************
	******************************
	{
	***COMPLETE***
	preserve 
	
			gen pre_SqM_Per_100SqM_0_to_1920= pre_SqM_Per_100SqM_0-pre_SqM_Per_100SqM_1920
			gen building_0_1920=(pre_SqM_Per_100SqM_0_to_1920>0)
			replace building_0_1920= (building_0_1920)*(((50^2)*_pi)/(((250^2)*_pi)-((50^2)*_pi))) if $d_c$low_high2 ==1
			gen new_y_build= post_building_$endyear - building_0_1920
			gen new_y_sqm= post_SqM_Per_100SqM_$endyear - pre_SqM_Per_100SqM_0_to_1920 
			
	ttest new_y_build if $d_c$low_high1 ==1, by(unplugged_treat) unequal 
	matrix a1= r(mu_2)
	matrix a2= r(mu_1)
	matrix a3= r(mu_2)-r(mu_1)
	matrix a4= r(p)
	
	ttest new_y_build if $d_c$low_high2 ==1, by(unplugged_treat) unequal 
	matrix a5= r(mu_2)
	matrix a6= r(mu_1)
	matrix a7= r(mu_2)-r(mu_1)
	matrix a8= r(p)
	
		regress new_y_build UP_$d_c$low_high1 $d_c$low_high1 unplugged_treat

	matrix a9==a1-a5
	matrix a10==a2-a6
	matrix a11= a3-a7
	matrix a12==0.000
	matrix a15==.
		
		ttest new_y_build if unplugged_treat==1, by(bldg_near_) unequal 
			matrix a13= r(p)
		ttest new_y_build if unplugged_treat==0, by(bldg_near_) unequal
			matrix a14= r(p)
			
	matrix a= a1,a2,a3,a4\a5,a6,a7,a8\a9,a10,a11,a12\a13,a14,a12,a15
	
	matrix colnames a = "Unplugged (N=1507)" "Plugged (N=2467)" "Difference" "P-Value"
	matrix rownames a = "Near (0 to 50m)" "Far (50 to 250m)" "Difference" "P-Value"
	
	esttab matrix(a, fmt(3)) using "C:\Users\maxha\Box\Abandoned Wells and Building\JAERE\Final Submission Files\uncond_means_binary.tex",  nomtitles replace	

	ttest new_y_sqm if $d_c$low_high1 ==1, by(unplugged_treat) unequal 
	matrix a1= r(mu_2)
	matrix a2= r(mu_1)
	matrix a3= r(mu_2)-r(mu_1)
	matrix a4= r(p)
	
	ttest new_y_sqm if $d_c$low_high2 ==1, by(unplugged_treat) unequal 
	matrix a5= r(mu_2)
	matrix a6= r(mu_1)
	matrix a7= r(mu_2)-r(mu_1)
	matrix a8= r(p)
	
		regress new_y_sqm UP_$d_c$low_high1 $d_c$low_high1 unplugged_treat

	matrix a9==a1-a5
	matrix a10==a2-a6
	matrix a11= a3-a7
	matrix a12==0.013
	matrix a15==.
		
		ttest new_y_sqm if unplugged_treat==1, by(bldg_near_) unequal 
			matrix a13= r(p)
		ttest new_y_sqm if unplugged_treat==0, by(bldg_near_) unequal
			matrix a14= r(p)

	matrix a= a1,a2,a3,a4\a5,a6,a7,a8\a9,a10,a11,a12\a13,a14,a12,a15
	
	matrix colnames a = "Unplugged (N=1507)" "Plugged (N=2467)" "Difference" "P-Value"
	matrix rownames a = "Near (0 to 50m)" "Far (50 to 250m)" "Difference" "P-Value"
	
	esttab matrix(a, fmt(3)) using "C:\Users\maxha\Box\Abandoned Wells and Building\JAERE\Final Submission Files\uncond_means_ftprnt.tex",  nomtitles replace	
	
	restore
	
	***COMPLETE PRE_1920***
	
	preserve 
		gen pre_SqM_Per_100SqM_0_to_1920= pre_SqM_Per_100SqM_0-pre_SqM_Per_100SqM_1920
		
		gen building_0_1920=(pre_SqM_Per_100SqM_0_to_1920>0)
			replace building_0_1920= (building_0_1920)*(((50^2)*_pi)/(((250^2)*_pi)-((50^2)*_pi))) if $d_c$low_high2 ==1
			
	ttest building_0_1920 if $d_c$low_high1 ==1, by(unplugged_treat) unequal 
	matrix a1= r(mu_2)
	matrix a2= r(mu_1)
	matrix a3= r(mu_2)-r(mu_1)
	matrix a4= r(p)
	
	ttest building_0_1920 if $d_c$low_high2 ==1, by(unplugged_treat) unequal 
	matrix a5= r(mu_2)
	matrix a6= r(mu_1)
	matrix a7= r(mu_2)-r(mu_1)
	matrix a8= r(p)
	
		regress building_0_1920 UP_$d_c$low_high1 $d_c$low_high1 unplugged_treat

	matrix a9==a1-a5
	matrix a10==a2-a6
	matrix a11= a3-a7
	matrix a12==0.575
	matrix a15==.
		
		ttest building_0_1920 if unplugged_treat==1, by(bldg_near_) unequal 
			matrix a13= r(p)
		ttest building_0_1920 if unplugged_treat==0, by(bldg_near_) unequal
			matrix a14= r(p)
			
	matrix a= a1,a2,a3,a4\a5,a6,a7,a8\a9,a10,a11,a12\a13,a14,a12,a15
	
	matrix colnames a = "Unplugged (N=1507)" "Plugged (N=2467)" "Difference" "P-Value"
	matrix rownames a = "Near (0 to 50m)" "Far (50 to 250m)" "Difference" "P-Value"
	
	esttab matrix(a, fmt(3)) using "C:\Users\maxha\Box\Abandoned Wells and Building\JAERE\Final Submission Files\pre20_uncond_means_binary.tex",  nomtitles replace	

	ttest pre_SqM_Per_100SqM_0_to_1920 if $d_c$low_high1 ==1, by(unplugged_treat) unequal 
	matrix a1= r(mu_2)
	matrix a2= r(mu_1)
	matrix a3= r(mu_2)-r(mu_1)
	matrix a4= r(p)
	
	ttest pre_SqM_Per_100SqM_0_to_1920 if $d_c$low_high2 ==1, by(unplugged_treat) unequal 
	matrix a5= r(mu_2)
	matrix a6= r(mu_1)
	matrix a7= r(mu_2)-r(mu_1)
	matrix a8= r(p)
	
		regress pre_SqM_Per_100SqM_0_to_1920 UP_$d_c$low_high1 $d_c$low_high1 unplugged_treat

	matrix a9==a1-a5
	matrix a10==a2-a6
	matrix a11= a3-a7
	matrix a12== 0.408
	matrix a15==.
		
		ttest pre_SqM_Per_100SqM_0_to_1920 if unplugged_treat==1, by(bldg_near_) unequal 
			matrix a13= r(p)
		ttest pre_SqM_Per_100SqM_0_to_1920 if unplugged_treat==0, by(bldg_near_) unequal
			matrix a14= r(p)

	matrix a= a1,a2,a3,a4\a5,a6,a7,a8\a9,a10,a11,a12\a13,a14,a12,a15
	
	matrix colnames a = "Unplugged (N=1507)" "Plugged (N=2467)" "Difference" "P-Value"
	matrix rownames a = "Near (0 to 50m)" "Far (50 to 250m)" "Difference" "P-Value"
	
	esttab matrix(a, fmt(3)) using "C:\Users\maxha\Box\Abandoned Wells and Building\JAERE\Final Submission Files\pre20_uncond_means_ftprnt.tex",  nomtitles replace	
	
	restore
		}

	******************************
	******************************
		* Well Date Figures 	*
	******************************
	******************************
	{
	preserve
			keep if bldg_near_==1
			
			sum date_plugg_year if date_plugg_year>1880, detail /*MEAN AND MEDIAN PLUGGING DATE*/
			
		*Figure on min year by decade
			tostring minyear, generate(minyear_str)
			gen minyear_dec= substr(minyear_str, 1,3)
			destring minyear_dec, replace
			gen minyear_decade= minyear_dec*10
			replace minyear_decade= 0 if minyear_decade==.
			drop minyear_dec
			replace minyear_decade=10000 if minyear_dec==1810
			tab minyear_decade, matcell(x)
			scalar N=r(N)
			
			matrix x= (x/N)*100
		
			*x axis
		
			matrix decades=1860\1870\1880\1890\1900\1910\1920\1930\1940\1950\1960\1970
		
			*graphing
			matrix a= x, decades
			svmat a, names(a)
			
			label define dlabel 1860 "1860" 1870 "1870" 1880 "1880" 1890 "1890" 1900 "1900" 1910 "1910" 1920 "1920" 1930 "1930" 1940 "1940" 1950 "1950" 1960 "1960" 1970 "Unknown"
			label values a2 dlabel
			
			gen a3 = sum(a1)
			replace a3=. if a1==.
			
					qui: graph bar (mean) a1, over(a2) ytitle("Percentage of Sample Wells", margin(medium) size(large)) ylab(0(5)25, labsize(medium) nogrid format(%9.0gc)) ysize(2.2)  /*
					*/ graphregion(margin(2 2 2 2) fcolor(white) lcolor(white)) bgcolor(white) plotregion(lstyle(none) fcolor(none)) bar(1,color(gs10)) blabel(total, size(medium) format(%2.1f))
		
					graph export "C:\Users\maxha\Box\Abandoned Wells and Building\JAERE\Final Submission Files\minyear_dist.jpg", width(4800) as(jpg) replace
			
		*Tabs on plugging by decade
			tostring date_plugg_year , generate(date_plugg_year_str)
			gen date_plugg_dec= substr(date_plugg_year_str, 1,3)
			destring date_plugg_dec, replace
			gen date_plugg_decade= date_plugg_dec*10
			replace date_plugg_decade= 0 if date_plugg_decade==.
			drop date_plugg_dec
			replace date_plugg_decade=10000 if date_plugg_decade==10
			tab date_plugg_decade if unplugged_treat==0, matcell(x) 
			scalar N=r(N)
			
			matrix x= (x/N)*100
		
			*x axis
			
			matrix decades=1870\1880\1890\1900\1910\1920\1930\1940\1950\1960\1970
			
			matrix b= x, decades
			svmat b, names(b)
			
			label define dlabel2 1870 "1870" 1880 "1880" 1890 "1890" 1900 "1900" 1910 "1910" 1920 "1920" 1930 "1930" 1940 "1940" 1950 "1950" 1960 "1960" 1970 "Unknown"
			label values b2 dlabel2
			
			gen b3 = sum(b1)
			replace b3=. if b1==.
			
			qui: graph bar (mean) b1, over(b2) ytitle("Percentage of Sample Wells", margin(medium) size(large)) ylab(0(5)35, labsize(medium) nogrid format(%9.0gc)) ysize(2.2)  /*
					*/ graphregion(margin(2 2 2 2) fcolor(white) lcolor(white)) bgcolor(white) plotregion(lstyle(none) fcolor(none)) bar(1,color(gs10)) blabel(total, size(medium) format(%2.1f))
		
					graph export "C:\Users\maxha\Box\Abandoned Wells and Building\JAERE\Final Submission Files\plugyear_dist.jpg", width(4800) as(jpg) replace
restore
					}
	******************************
	******************************
	*BEGIN 0 to 50, 50 to 250
			*REGRESSIONS
	******************************
	******************************
		{	
			*************************
			*************************
			/*THE TRIPLE DIFF SETUP*/
			*************************
			*************************
			gen pre_SqM_Per_100SqM_0_to_1920= pre_SqM_Per_100SqM_0-pre_SqM_Per_100SqM_1920
				gen building_0_1920=(pre_SqM_Per_100SqM_0_to_1920>0)
				replace building_0_1920= (building_0_1920)*(((50^2)*_pi)/(((250^2)*_pi)-((50^2)*_pi))) if $d_c$low_high2 ==1
			
			gen pre_res_SqM_Per_100SqM_0_to_1920= pre_res_SqM_Per_100SqM_0-pre_res_SqM_Per_100SqM_1920
				gen building_res_0_1920=(pre_res_SqM_Per_100SqM_0_to_1920>0)
				replace building_res_0_1920= (building_res_0_1920)*(((50^2)*_pi)/(((250^2)*_pi)-((50^2)*_pi))) if $d_c$low_high2 ==1

			gen pre_com_SqM_Per_100SqM_0_to_1920= pre_com_SqM_Per_100SqM_0-pre_com_SqM_Per_100SqM_1920
				gen building_com_0_1920=(pre_com_SqM_Per_100SqM_0_to_1920>0)
				replace building_com_0_1920= (building_com_0_1920)*(((50^2)*_pi)/(((250^2)*_pi)-((50^2)*_pi))) if $d_c$low_high2 ==1

			gen pre_out_SqM_Per_100SqM_0_to_1920= pre_out_SqM_Per_100SqM_0-pre_out_SqM_Per_100SqM_1920
				gen building_out_0_1920=(pre_out_SqM_Per_100SqM_0_to_1920>0)
				replace building_out_0_1920= (building_out_0_1920)*(((50^2)*_pi)/(((250^2)*_pi)-((50^2)*_pi))) if $d_c$low_high2 ==1

			expand 2
			
			bysort well_id $d_c$low_high1 : gen post=(_n==1)
			order well_id post $d_c$low_high1
			
			
			replace post_building_$endyear = building_0_1920 if post==0
			replace building_res_1970_$endyear = building_res_0_1920 if post==0
			replace building_com_1970_$endyear  = building_com_0_1920 if post==0
			replace building_out_1970_$endyear = building_out_0_1920 if post==0
			replace post_SqM_Per_100SqM_$endyear = pre_SqM_Per_100SqM_0_to_1920 if post==0
			replace post_res_SqM_Per_100SqM_$endyear = pre_res_SqM_Per_100SqM_0_to_1920 if post==0
			replace post_com_SqM_Per_100SqM_$endyear = pre_com_SqM_Per_100SqM_0_to_1920 if post==0
			replace post_out_SqM_Per_100SqM_$endyear = pre_out_SqM_Per_100SqM_0_to_1920 if post==0
			
			gen post_up= post*unplugged_treat
			gen post_near= post * $d_c$low_high1
			gen post_near_up= post * $d_c$low_high1 * unplugged_treat
			
			

			*************************
			*************************
					/*THE REGS*/
			*************************
			*************************
			
			
			regress post_building_$endyear   post_near_up post_near post_up UP_$d_c$low_high1 post unplugged_treat $d_c$low_high1 , cluster (well_id) 
				estimates store win1
				
				count if unplugged_treat==1
				estadd local nup=(r(N)/4) 
				count if unplugged_treat==0
				estadd local np=(r(N)/4)
	
			regress post_SqM_Per_100SqM_$endyear post_near_up post_near post_up UP_$d_c$low_high1 post unplugged_treat $d_c$low_high1 , cluster (well_id) 
				estimates store win3
				
				count if unplugged_treat==1
				estadd local nup=(r(N) /4) 
				count if unplugged_treat==0
				estadd local np=(r(N) /4)
				
					esttab win1 win3 using "C:\Users\maxha\Box\Abandoned Wells and Building\JAERE\Final Submission Files\reg_ftprnt.tex", ///
					label cells(b(nostar fmt(3)) se(par fmt(3))) keep(post_near_up post_near post_up UP_$d_c$low_high1 post $d_c$low_high1 unplugged_treat _cons) ///
					coeflabels(post_near_up "Post X Near X Unplugged" post_near "Post X Near" post_up "Post X Unplugged" UP_$d_c$low_high1 "Near X Unplugged" post "Post" $d_c$low_high1 "Near (0 to 50 m)" unplugged_treat "Unplugged" _cons "Intercept") ///
					collabels(none) nomtitles nodepvars ///
					order(post_near_up post_near post_up UP_$d_c$low_high1 post $d_c$low_high1 unplugged_treat _cons) ///
					stats(r2_a N nup np, fmt(2 %9.0f) label("R-squared" "Observations (Four per Well)" "N Unplugged Wells" "N Plugged Wells" )) ///
					mgroups("Probability of Building" "Building Footprint", pattern(1 1) span prefix(\multicolumn{@span}{c}{) suffix(})) ///
					replace
		
			
		**Residential, Commercial, Out   
			
			regress building_res_1970_$endyear post_near_up post_near post_up UP_$d_c$low_high1 post unplugged_treat $d_c$low_high1 , cluster(well_id) 
				estimates store win71
				
				count if unplugged_treat==1
				estadd local nup=(r(N) /4) 
				count if unplugged_treat==0
				estadd local np=(r(N) /4)
			regress building_com_1970_$endyear post_near_up post_near post_up UP_$d_c$low_high1 post unplugged_treat $d_c$low_high1 , cluster(well_id) 
				estimates store win81
				
				count if unplugged_treat==1
				estadd local nup=(r(N) /4) 
				count if unplugged_treat==0
				estadd local np=(r(N) /4)
			regress building_out_1970_$endyear  post_near_up post_near post_up UP_$d_c$low_high1 post unplugged_treat $d_c$low_high1 , cluster(well_id) 
				estimates store win91
				
				count if unplugged_treat==1
				estadd local nup=(r(N) /4) 
				count if unplugged_treat==0
				estadd local np=(r(N) /4)
			regress post_res_SqM_Per_100SqM_$endyear post_near_up post_near post_up UP_$d_c$low_high1 post unplugged_treat $d_c$low_high1 , cluster(well_id) 
				estimates store win7
				
				count if unplugged_treat==1
				estadd local nup=(r(N) /4) 
				count if unplugged_treat==0
				estadd local np=(r(N) /4)
			regress post_com_SqM_Per_100SqM_$endyear post_near_up post_near post_up UP_$d_c$low_high1 post unplugged_treat $d_c$low_high1 , cluster(well_id) 
				estimates store win8
				
				count if unplugged_treat==1
				estadd local nup=(r(N) /4) 
				count if unplugged_treat==0
				estadd local np=(r(N) /4)
			regress post_out_SqM_Per_100SqM_$endyear post_near_up post_near post_up UP_$d_c$low_high1 post unplugged_treat $d_c$low_high1 , cluster(well_id) 
				estimates store win9
				
				count if unplugged_treat==1
				estadd local nup=(r(N) /4) 
				count if unplugged_treat==0
				estadd local np=(r(N) /4)
				
					esttab win71 win81 win91 win7 win8 win9 using "C:\Users\maxha\Box\Abandoned Wells and Building\JAERE\Final Submission Files\reg_ftprnt_type.tex", ///
					label cells(b(nostar fmt(3)) se(par fmt(3))) keep(post_near_up post_near post_up UP_$d_c$low_high1 post $d_c$low_high1 unplugged_treat _cons) ///
					coeflabels(post_near_up "Post X Near X Unplugged" post_near "Post X Near" post_up "Post X Unplugged" UP_$d_c$low_high1 "Near X Unplugged" post "Post" $d_c$low_high1 "Near (0 to 50 m)" _cons "Intercept" unplugged_treat "Unplugged") ///
					collabels(none) mtitles("Residential" "Commercial" "Out" "Residential" "Commercial" "Out") nodepvars ///
					order(post_near_up post_near post_up UP_$d_c$low_high1 post $d_c$low_high1 unplugged_treat _cons) ///
					stats(r2_a N nup np, fmt(2 %9.0f) label("R-squared" "Observations (Four per Well)" "N Unplugged Wells" "N Plugged Wells" )) ///
					mgroups("Probability of Building" "Building Footprint", pattern(1 0 0 1 0 0) span prefix(\multicolumn{@span}{c}{) suffix(})) ///
					replace
					 
		**Subsamples
				regress post_building_$endyear post_near_up post_near post_up UP_$d_c$low_high1 post unplugged_treat $d_c$low_high1 if (date_plugg_year<1960 | unplugged_treat==1), cluster(well_id) /*JUMP HERE*/
				estimates store win10
				
				count if unplugged_treat==1
				estadd local nup=(r(N) /4) 
				count if unplugged_treat==0
				estadd local np=(r(N) /4)
				
				regress post_building_$endyear post_near_up post_near post_up UP_$d_c$low_high1 post unplugged_treat $d_c$low_high1 if (Nearest_Co<300), cluster(well_id) 
				estimates store win11
				
				count if unplugged_treat==1 & Nearest_Co<300
				estadd local nup=(r(N) /4) 
				count if unplugged_treat==0 & Nearest_Co<300
				estadd local np=(r(N) /4)
				
				regress post_SqM_Per_100SqM_$endyear post_near_up post_near post_up UP_$d_c$low_high1 post unplugged_treat $d_c$low_high1 if (date_plugg_year<1960 | unplugged_treat==1), cluster(well_id) /*JUMP HERE*/
				estimates store win101
				
				count if unplugged_treat==1
				estadd local nup=(r(N) /4) 
				count if unplugged_treat==0
				estadd local np=(r(N) /4)
				
				regress post_SqM_Per_100SqM_$endyear post_near_up post_near post_up UP_$d_c$low_high1 post unplugged_treat $d_c$low_high1 if (Nearest_Co<300), cluster(well_id) 
				estimates store win111
				
				count if unplugged_treat==1 & Nearest_Co<300
				estadd local nup=(r(N) /4) 
				count if unplugged_treat==0 & Nearest_Co<300
				estadd local np=(r(N) /4)
				
				esttab win10 win11 win101 win111 using "C:\Users\maxha\Box\Abandoned Wells and Building\JAERE\Final Submission Files\reg_subsamp.tex", ///
					label cells(b(nostar fmt(3)) se(par fmt(3))) keep(post_near_up post_near post_up UP_$d_c$low_high1 post $d_c$low_high1 unplugged_treat _cons) ///
					coeflabels(post_near_up "Post X Near X Unplugged" post_near "Post X Near" post_up "Post X Unplugged" UP_$d_c$low_high1 "Near X Unplugged" post "Post" $d_c$low_high1 "Near (0 to 50 m)" _cons "Intercept" unplugged_treat "Unplugged") ///
					collabels(none) mtitles("Plugged Pre-1960" "Near Coal" "Plugged Pre-1960" "Near Coal") nodepvars ///
					order(post_near_up post_near post_up UP_$d_c$low_high1 post $d_c$low_high1 unplugged_treat _cons) ///
					stats(r2_a N nup np, fmt(2 %9.0f) label("R-squared" "Observations (Four per Well)" "N Unplugged Wells" "N Plugged Wells" )) ///
					mgroups("Probability of Building" "Building Footprint", pattern(1 0 1 0) span prefix(\multicolumn{@span}{c}{) suffix(})) ///
					replace
					
		*Variation within Plugged Group
		preserve
			
			gen sample= (minyear<1920 & minyear>1850)
			keep if sample==1
			drop if unplugged_treat==1
			gen plugged_pre_1920=1 if (date_plugg_year<1920 & date_plugg_year>1850) | (date_plugg_year<1850 & minyear<=1900)
			replace plugged_pre_1920=0 if (date_plugg_year>=1920 & date_plugg_year<=1970) | (date_plugg_year<1850 & minyear>1900)
			drop if plugged_pre_1920==. /*SHOULD BE A ZERO*/
			tab plugged_pre_1920

			gen plug_y_build= pre_building_1920 - building_0_1920
			gen plug_y_sqm= pre_SqM_Per_100SqM_1920 - pre_SqM_Per_100SqM_0_to_1920
			replace plug_y_build = building_0_1920 if post==0
			replace plug_y_sqm =  pre_SqM_Per_100SqM_0_to_1920 if post==0	
		
			gen post_pp=(post*plugged_pre_1920)
			gen post_near_pp=(post * $d_c$low_high1 * plugged_pre_1920)
			gen near_pp=(plugged_pre_1920*$d_c$low_high1)
			
			regress plug_y_build post_near_pp post_near post_pp near_pp post plugged_pre_1920 $d_c$low_high1 , cluster(well_id) 
				estimates store win12
				
				count if plugged_pre_1920==1
				estadd local nup=(r(N) /4) 
				count if plugged_pre_1920==0
				estadd local np=(r(N) /4)
			regress post_building_$endyear  post_near_pp post_near post_pp near_pp post plugged_pre_1920 $d_c$low_high1 , cluster(well_id) 
				estimates store win13
				
				count if plugged_pre_1920==1
				estadd local nup=(r(N) /4) 
				count if plugged_pre_1920==0
				estadd local np=(r(N) /4)
			regress plug_y_sqm post_near_pp post_near post_pp near_pp post plugged_pre_1920 $d_c$low_high1 , cluster(well_id) 
				estimates store win14
				
				count if plugged_pre_1920==1
				estadd local nup=(r(N) /4) 
				count if plugged_pre_1920==0
				estadd local np=(r(N) /4)
			regress post_SqM_Per_100SqM_$endyear post_near_pp post_near post_pp near_pp post plugged_pre_1920 $d_c$low_high1 , cluster(well_id) 
				estimates store win15
				
				count if plugged_pre_1920==1
				estadd local nup=(r(N) /4) 
				count if plugged_pre_1920==0
				estadd local np=(r(N) /4)
				
				esttab win12 win13 win14 win15 using "C:\Users\maxha\Box\Abandoned Wells and Building\JAERE\Final Submission Files\reg_plugged.tex", ///
					label cells(b(nostar fmt(3)) se(par fmt(3))) keep(post_near_pp post_near post_pp near_pp post $d_c$low_high1 plugged_pre_1920 _cons) ///
					coeflabels(post_near_pp "Post X Near X Plugged Pre-1920" post_near "Post X Near" post_pp "Post X Plugged Pre-1920" near_pp "Near X Plugged Pre-1920" post "Post" $d_c$low_high1 "Near (0 to 50 m)" _cons "Intercept" plugged_pre_1920 "Plugged Pre-1920 ") ///
					collabels(none) mtitles("1920-1970" "1970-2017" "1920-1970" "1970-2017") nodepvars ///
					order(post_near_pp post_near post_pp near_pp post $d_c$low_high1 plugged_pre_1920 _cons) ///
					stats(r2_a N nup np, fmt(2 %9.0f) label("R-squared" "Observations (Four per Well)" "N Plugged Pre-1920" "N Plugged 1920-1969" )) ///
					mgroups("Building" "Footprint", pattern(1 0 1 0) span prefix(\multicolumn{@span}{c}{) suffix(})) ///
					replace
		restore
		
		*Shifting
			preserve
				
				drop if unplugged==1 
							
				gen post_pp=(post*unplug_pre_well_win_50_250_0)
				gen post_near_pp=(post * $d_c$low_high1 * unplug_pre_well_win_50_250_0)
				gen near_pp=(unplug_pre_well_win_50_250_0*$d_c$low_high1)
			
				regress post_SqM_Per_100SqM_$endyear post_near_pp post_near post_pp near_pp post unplug_pre_well_win_50_250_0 $d_c$low_high1 , cluster(well_id) 
				estimates store win16
				
				count
				estadd local np=(r(N) /4)
				
				regress post_out_SqM_Per_100SqM_$endyear post_near_pp post_near post_pp near_pp post unplug_pre_well_win_50_250_0 $d_c$low_high1 , cluster(well_id) 
				estimates store win17
				
				count
				estadd local np=(r(N) /4)
				
					
					esttab win16 win17 using "C:\Users\maxha\Box\Abandoned Wells and Building\JAERE\Final Submission Files\reg_shifting.tex", ///
					label cells(b(nostar fmt(3)) se(par fmt(3))) keep(post_near_pp post_near post_pp near_pp post $d_c$low_high1 _cons) ///
					coeflabels(post_near_pp "Post X Near X Number UP Wells (50 to 250 m)" post_near "Post X Near" post_pp "Post X Number UP Wells (50 to 250 m)" near_pp "Near X Number UP Wells (50 to 250 m)" post "Post" $d_c$low_high1 "Near (0 to 50 m)" _cons "Intercept") ///
					collabels(none) nomtitles nodepvars ///
					order(post_near_pp post_near post_pp near_pp post $d_c$low_high1 _cons) ///
					stats(r2_a N np, fmt(2 %9.0f) label("R-squared" "Observations (Four per Well)" "N Plugged Wells")) ///
					mgroups("All Footprint" "Out Building Footprint", pattern(1 1) span prefix(\multicolumn{@span}{c}{) suffix(})) ///
					replace
			restore
			
		*Long Term
		
		preserve
			
			gen sample= (minyear<1920 & minyear>1850)
			keep if sample==1
			gen plugged_pre_1920=1 if (date_plugg_year<1920 & date_plugg_year>1850) | (date_plugg_year<1850 & minyear<=1900)
			replace plugged_pre_1920=0 if (date_plugg_year>=1920 & date_plugg_year<=1970) | (date_plugg_year<1850 & minyear>1900)
			gen sample4= (plugged_pre_1920==1  |unplugged_treat==1)
			gen sample5= (plugged_pre_1920==0   |unplugged_treat==1)
			
			gen SqM_Per_100SqM_1920_2017=pre_SqM_Per_100SqM_1920+post_SqM_Per_100SqM_2017
			
			gen building_1920_1970=(pre_SqM_Per_100SqM_1920>0)
				replace building_1920_1970= (building_1920_1970)*(((50^2)*_pi)/(((250^2)*_pi)-((50^2)*_pi))) if $d_c$low_high2 ==1
			gen building_1920_2017=(SqM_Per_100SqM_1920_2017>0)
				replace building_1920_2017= (building_1920_2017)*(((50^2)*_pi)/(((250^2)*_pi)-((50^2)*_pi))) if $d_c$low_high2 ==1
				
			replace building_1920_1970=building_0_1920 if post==0
			replace building_1920_2017=building_0_1920 if post==0
			replace pre_SqM_Per_100SqM_1920= pre_SqM_Per_100SqM_0_to_1920 if post==0
			replace SqM_Per_100SqM_1920_2017=pre_SqM_Per_100SqM_0_to_1920 if post==0
						
			regress building_1920_2017 post_near_up post_near post_up UP_$d_c$low_high1 post unplugged_treat $d_c$low_high1 , cluster(well_id) 
				estimates store win18
				
				count if unplugged_treat==1
				estadd local nup=(r(N)/4) 
				count if unplugged_treat==0
				estadd local np=(r(N)/4)
			regress building_1920_2017 post_near_up post_near post_up UP_$d_c$low_high1 post unplugged_treat $d_c$low_high1 if sample4==1, cluster(well_id) 
				estimates store win19
				
				count if unplugged_treat==1  & sample4==1
				estadd local nup=(r(N)/4) 
				count if unplugged_treat==0  & sample4==1
				estadd local np=(r(N)/4)
			regress building_1920_2017 post_near_up post_near post_up UP_$d_c$low_high1 post unplugged_treat $d_c$low_high1 if sample5==1, cluster(well_id) 
				estimates store win20
				
				count if unplugged_treat==1  & sample5==1
				estadd local nup=(r(N)/4) 
				count if unplugged_treat==0  & sample5==1
				estadd local np=(r(N)/4)
				
			regress SqM_Per_100SqM_1920_2017 post_near_up post_near post_up UP_$d_c$low_high1 post unplugged_treat $d_c$low_high1 , cluster(well_id) 
				estimates store win21
				
				count if unplugged_treat==1
				estadd local nup=(r(N)/4) 
				count if unplugged_treat==0
				estadd local np=(r(N)/4)
			regress SqM_Per_100SqM_1920_2017 post_near_up post_near post_up UP_$d_c$low_high1 post unplugged_treat $d_c$low_high1 if sample4==1, cluster(well_id) 
				estimates store win22
				
				count if unplugged_treat==1 & sample4==1
				estadd local nup=(r(N)/4) 
				count if unplugged_treat==0 & sample4==1
				estadd local np=(r(N)/4)
			regress SqM_Per_100SqM_1920_2017 post_near_up post_near post_up UP_$d_c$low_high1 post unplugged_treat $d_c$low_high1 if sample5==1, cluster(well_id) 
				estimates store win23
				
				count if unplugged_treat==1 & sample5==1
				estadd local nup=(r(N)/4) 
				count if unplugged_treat==0 & sample5==1
				estadd local np=(r(N)/4)
			
			esttab  win19 win20 win18 win22 win23 win21 using "C:\Users\maxha\Box\Abandoned Wells and Building\JAERE\Final Submission Files\reg_lt.tex", ///
					label cells(b(nostar fmt(3)) se(par fmt(3))) keep(post_near_up post_near post_up UP_$d_c$low_high1 post $d_c$low_high1 unplugged_treat _cons) ///
					coeflabels(post_near_up "Post X Near X Unplugged" post_near "Post X Near" post_up "Post X Unplugged" UP_$d_c$low_high1 "Near X Unplugged" post "Post" $d_c$low_high1 "Near (0 to 50 m)" _cons "Intercept" unplugged_treat "Unplugged") ///
					collabels(none) mtitles("Plugged Pre" "Plugged Post" "All Pre-1920" "Plugged Pre" "Plugged Post" "All Pre-1920") nodepvars ///
					order(post_near_up post_near post_up UP_$d_c$low_high1 post $d_c$low_high1 unplugged_treat _cons) ///
					stats(r2_a N nup np, fmt(2 %9.0f) label("R-squared" "Observations (Four per Well)" "N Unplugged Wells" "N Plugged Wells" )) ///
					mgroups("Probability of Building" "Building Footprint", pattern(1 0 0 1 0 0) span prefix(\multicolumn{@span}{c}{) suffix(})) ///
					replace			
		restore	
		
		*Overlap
		preserve
			keep if other_conv_well_0_100==0
			drop if $d_c$low_high2 ==1
			
			regress post_building_$endyear  post_up post unplugged_treat , cluster(well_id) 
					estimates store win203
					
					count if unplugged_treat==1
					estadd local nup=(r(N)/2) 
					count if unplugged_treat==0
					estadd local np=(r(N)/2)
			
			regress post_SqM_Per_100SqM_$endyear post_up post unplugged_treat , cluster(well_id) 
					estimates store win204
					
					count if unplugged_treat==1
					estadd local nup=(r(N) /2) 
					count if unplugged_treat==0
					estadd local np=(r(N) /2)
					
					esttab win203 win204 using "C:\Users\maxha\Box\Abandoned Wells and Building\JAERE\Final Submission Files\reg_overlap2.tex", ///
					label cells(b(nostar fmt(3)) se(par fmt(3))) keep(post_up post unplugged_treat _cons) ///
					coeflabels(post_up "Post X Unplugged" post "Post" unplugged_treat "Unplugged" _cons "Intercept") ///
					collabels(none) nomtitles nodepvars ///
					order(post_up post unplugged_treat _cons) ///
					stats(r2_a N nup np, fmt(2 %9.0f) label("R-squared" "Observations (Two per Well)" "N Unplugged Wells" "N Plugged Wells" )) ///
					mgroups("Probability of Building" "Building Footprint", pattern(1 1) span prefix(\multicolumn{@span}{c}{) suffix(})) ///
					replace

		restore
}

*******************************************
*******************************************
*			MAIN ANALYSIS				  *
*			   Spatial Gradient			  *
*			   							  *
*******************************************
*******************************************		
{
	clear
	clear matrix
	clear mata
	set maxvar 10000
	set more off
	*cd "C:\Users\mrh105\Box\EDWIN_DEP_Well_Data\Washington_County_Project\Circle and Doughnut Tables" /*PC*/
	cd "C:\Users\maxha\Box\EDWIN_DEP_Well_Data\Washington_County_Project\Final Code\Circle and Doughnut Tables" /*Home PC*/

	*END YEAR MACRO

		/*TURNS ON 2005 END YEAR*/
			*global endyear = "2005"
		/*TURNS ON 2017 END YEAR*/
			global endyear = "2017"
		/*TURNS ON UNLIMITED START YEAR */
			global startyear = "0"
		/*TURNS ON 1920 START YEAR*/
			*global startyear = "1920"

	*CIRCLE/DOUGHNUT/NEAR_FAR MACROS
		
		*Turns on doughnut globals, must also turn on doughnut dependent variables and turn off either circle table or near_far table
		global d_c= "doughnut_"
		global low_high1 = "0_50"
		global low_high2 = "_50_100"
		global low_high3 = "100_150"
		global low_high4 = "150_200"
		global low_high5 = "200_250"
		
	use Table_$d_c$low_high1$low_high2.dta
		
	*SAMPLE CONSTRUCTION
		drop if minyear>=1970 /*Only including Pre-1970 Wells in the regressions*/
		replace bplugged1=0 if (bplugged1==1 & last_produ_report_year>=1970 & last_produ_report_year!=.) | (bplugged1==1 & first_produ_report_year>=1970 & first_produ_report_year!=.)| (bplugged1==1 & wellstatus=="DEP PLUGGED")  /*29 wells without plugging dates were likely plugged recently and must be dropped in the next line*/
		keep if bplugged1==1 | bunplugged$endyear ==1 /*Keeping only wells plugged for the entire period, or not plugged for the entire period. Getting rid of "switchers"*/
		drop if Nearest_Ci<=300 /*Dropping those observations in or within 300 m from Washington or Canonsburg*/
		keep if oil_type==1 | gas_type==1 | oil2_type==1 /*Keeping only oil, gas, and oil and gas wells*/

	*TREATMENT VARIABLE
		rename bunplugged$endyear unplugged_treat
		tab unplugged_treat /*1's are unplugged for the whole period, zeros are plugged for the whole period*/

	*DOUGHNUT DEPENDENT VARIABLES

		foreach n in 2005 2017  {
			gen post_building_`n'=(post_build_`n'>0)
				replace post_building_`n'= (post_building_`n')*(((50^2)*_pi)/(((100^2)*_pi)-((50^2)*_pi))) if $d_c$low_high2 ==1
				replace post_building_`n'= (post_building_`n')*(((50^2)*_pi)/(((150^2)*_pi)-((100^2)*_pi))) if $d_c$low_high3 ==1
				replace post_building_`n'= (post_building_`n')*(((50^2)*_pi)/(((200^2)*_pi)-((150^2)*_pi))) if $d_c$low_high4 ==1
				replace post_building_`n'= (post_building_`n')*(((50^2)*_pi)/(((250^2)*_pi)-((200^2)*_pi))) if $d_c$low_high5 ==1
			gen post_SqM_Per_100SqM_`n'= (post_SqM_`n')/((50^2)*_pi*(1/100))
				replace post_SqM_Per_100SqM_`n'= (post_SqM_`n')/((((100^2)*_pi)-((50^2)*_pi))*(1/100)) if $d_c$low_high2 ==1
				replace post_SqM_Per_100SqM_`n'= (post_SqM_`n')/((((150^2)*_pi)-((100^2)*_pi))*(1/100)) if $d_c$low_high3 ==1
				replace post_SqM_Per_100SqM_`n'= (post_SqM_`n')/((((200^2)*_pi)-((150^2)*_pi))*(1/100)) if $d_c$low_high4 ==1
				replace post_SqM_Per_100SqM_`n'= (post_SqM_`n')/((((250^2)*_pi)-((200^2)*_pi))*(1/100)) if $d_c$low_high5 ==1
			}
	
		foreach s in 0 1920 {
			gen pre_building_`s'=(pre_build_y`s'>0)
				replace pre_building_`s'= (pre_building_`s')*(((50^2)*_pi)/(((100^2)*_pi)-((50^2)*_pi))) if $d_c$low_high2 ==1
				replace pre_building_`s'= (pre_building_`s')*(((50^2)*_pi)/(((150^2)*_pi)-((100^2)*_pi))) if $d_c$low_high3 ==1
				replace pre_building_`s'= (pre_building_`s')*(((50^2)*_pi)/(((200^2)*_pi)-((150^2)*_pi))) if $d_c$low_high4 ==1
				replace pre_building_`s'= (pre_building_`s')*(((50^2)*_pi)/(((250^2)*_pi)-((200^2)*_pi))) if $d_c$low_high5 ==1
				
			gen pre_SqM_Per_100SqM_`s'= (pre_SqM_y`s')/((50^2)*_pi*(1/100))
				replace pre_SqM_Per_100SqM_`s'= (pre_SqM_y`s')/((((100^2)*_pi)-((50^2)*_pi))*(1/100)) if $d_c$low_high2 ==1
				replace pre_SqM_Per_100SqM_`s'= (pre_SqM_y`s')/((((150^2)*_pi)-((100^2)*_pi))*(1/100)) if $d_c$low_high3 ==1
				replace pre_SqM_Per_100SqM_`s'= (pre_SqM_y`s')/((((200^2)*_pi)-((150^2)*_pi))*(1/100)) if $d_c$low_high4 ==1
				replace pre_SqM_Per_100SqM_`s'= (pre_SqM_y`s')/((((250^2)*_pi)-((200^2)*_pi))*(1/100)) if $d_c$low_high5 ==1
				
				gen pre_SqM_Per_100SqM_`s'_2= pre_SqM_Per_100SqM_`s'^2
}
					
	*INTERACTION TERMS
		gen UP_$d_c$low_high1 = $d_c$low_high1 * unplugged_treat
		gen UP_$d_c$low_high2 = $d_c$low_high2 * unplugged_treat
		gen UP_$d_c$low_high3 = $d_c$low_high3 * unplugged_treat
		gen UP_$d_c$low_high4 = $d_c$low_high4 * unplugged_treat
		gen UP_$d_c$low_high5 = $d_c$low_high5 * unplugged_treat
		
	*MACROS
		global binaries $d_c$low_high1 $d_c$low_high2 $d_c$low_high3 $d_c$low_high4
		global interactions UP_$d_c$low_high1 UP_$d_c$low_high2 UP_$d_c$low_high3 UP_$d_c$low_high4
		global landchars soil_WB_vrylim slope
		global nearest Nearest_Wa Nearest_79 Nearest_Park_Gameland Nearest_Co
		global nearby_endyear unplug_pre_well_win_250_0 unplug_post_well_win_250_$endyear plug_pre_well_win_250_0 plug_post_well_win_250_$endyear
		global pre_build pre_building_$startyear pre_SqM_Per_100SqM_$startyear pre_SqM_Per_100SqM_0_2
		*global controls $landchars $nearest $nearby_endyear $pre_build
		global controls $landchars $nearest $nearby_endyear
}
	******************************
	******************************
	*BEGIN SPATIAL GRADIENT
			*REGRESSIONS
	******************************
	******************************
		{
		
			
			gen pre_SqM_Per_100SqM_0_to_1920= pre_SqM_Per_100SqM_0-pre_SqM_Per_100SqM_1920
			gen building_0_1920=(pre_SqM_Per_100SqM_0_to_1920>0)
				replace building_0_1920= (building_0_1920)*(((50^2)*_pi)/(((100^2)*_pi)-((50^2)*_pi))) if $d_c$low_high2 ==1
				replace building_0_1920= (building_0_1920)*(((50^2)*_pi)/(((150^2)*_pi)-((100^2)*_pi))) if $d_c$low_high3 ==1
				replace building_0_1920= (building_0_1920)*(((50^2)*_pi)/(((200^2)*_pi)-((150^2)*_pi))) if $d_c$low_high4 ==1
				replace building_0_1920= (building_0_1920)*(((50^2)*_pi)/(((250^2)*_pi)-((200^2)*_pi))) if $d_c$low_high5 ==1
			gen new_y_build= post_building_$endyear - building_0_1920
			gen new_y_sqm= post_SqM_Per_100SqM_$endyear - pre_SqM_Per_100SqM_0_to_1920 
						
		preserve
			expand 2
			
			bysort well_id bldg_doughnut_ : gen post=(_n==1)
			order well_id post bldg_doughnut_
			
			replace post_building_$endyear = building_0_1920 if post==0
			replace post_SqM_Per_100SqM_$endyear = pre_SqM_Per_100SqM_0_to_1920 if post==0
			
				*INTERACTION TERMS
			gen post_up= post*unplugged_treat

			gen post_$d_c$low_high1 = $d_c$low_high1 * post
			gen post_$d_c$low_high2 = $d_c$low_high2 * post
			gen post_$d_c$low_high3 = $d_c$low_high3 * post
			gen post_$d_c$low_high4 = $d_c$low_high4 * post
			gen post_$d_c$low_high5 = $d_c$low_high5 * post
			
			gen up_post_$d_c$low_high1 = $d_c$low_high1 * post* unplugged_treat
			gen up_post_$d_c$low_high2 = $d_c$low_high2 * post* unplugged_treat
			gen up_post_$d_c$low_high3 = $d_c$low_high3 * post* unplugged_treat
			gen up_post_$d_c$low_high4 = $d_c$low_high4 * post* unplugged_treat
			gen up_post_$d_c$low_high5 = $d_c$low_high5 * post* unplugged_treat
			
			global triple_interactions up_post_$d_c$low_high1 up_post_$d_c$low_high2 up_post_$d_c$low_high3 up_post_$d_c$low_high4  UP_$d_c$low_high1 UP_$d_c$low_high2 UP_$d_c$low_high3 UP_$d_c$low_high4 post_$d_c$low_high1 post_$d_c$low_high2 post_$d_c$low_high3 post_$d_c$low_high4 post_up

			
			regress post_building_$endyear  $triple_interactions $binaries post unplugged_treat, cluster(well_id) 
			regress post_SqM_Per_100SqM_$endyear $triple_interactions $binaries post unplugged_treat, cluster(well_id) 

		restore
		
		preserve
			
			regress new_y_build $interactions $binaries unplugged_treat, cluster(well_id) 
				estimates store win1

				matrix b=e(b)'
				matrix doughnuts=1\2\3\4
				matrix d=(vecdiag(e(V)))'
				matmap d se,map(sqrt(@))
				matrix ci_u=b+1.96*se
				matrix ci_l=b-1.96*se
				matrix r= b,ci_l,ci_u
				svmat r, names(a)
				svmat doughnuts, names(d)
				
				label define dlabel 1 "0 to 50m" 2 "50 to 100m" 3 "100 to 150m" 4 "150 to 200m" 5 "200 to 250m"
				label values d1 dlabel
				
				replace a1=. if d1==.
				replace a2=. if d1==.
				replace a3=. if d1==.

				format  %9.3f a1
				
				qui: scatter a1 a2 a3 d1, ytitle("Probability of Building", size(large) margin(medium)) ylab(-.1 (.01)0, nogrid format(%9.0gc) /*
				*/ labsize(large) angle(0)) yline(0,lstyle(foreground)) ysize(3.5) xtitle("Location", size(large) margin(medium)) xlab(1(1)4, valuelabel labsize(medium)) xsize(6) /*
				*/ legend(off) lcolor(black black black) lpattern(solid dash dash) lwidth(thick thick thick)  /*
				*/ graphregion(lcolor(white) fcolor(white)) bgcolor(white) plotregion(lstyle(none) fcolor(none)) /*
				*/ graphregion(margin(2 10 2 2)) connect(1 1 1) mlabel(a1) msymbol(S none none) /*
				*/  mlabposition(5) mlabcolor(gs0)  mlabgap(1) mcolor(gs0)
				graph export "C:\Users\maxha\Box\Abandoned Wells and Building\JAERE\Final Submission Files\reg_share_spatial.jpg", width(4800) as(jpg) replace
						
		restore
		
		preserve
			
			regress new_y_sqm $interactions $binaries unplugged_treat, cluster(well_id) 
				estimates store win2

				matrix b=e(b)'
				matrix doughnuts=1\2\3\4
				matrix d=(vecdiag(e(V)))'
				matmap d se,map(sqrt(@))
				matrix ci_u=b+1.96*se
				matrix ci_l=b-1.96*se
				matrix r= b,ci_l,ci_u
				svmat r, names(a)
				svmat doughnuts, names(d)
				
				label define dlabel 1 "0 to 50m" 2 "50 to 100m" 3 "100 to 150m" 4 "150 to 200m" 5 "200 to 250m"
				label values d1 dlabel
				
				replace a1=. if d1==.
				replace a2=. if d1==.
				replace a3=. if d1==.

				format  %9.2f a1
				
				qui: scatter a1 a2 a3 d1, ytitle("Building Footprint (SqM Per 100 SqM)", size(large) margin(medium)) ylab(-1.5 (.25).25, nogrid format(%9.0gc) /*
				*/ labsize(large) angle(0)) yline(0,lstyle(foreground)) ysize(3.5) xtitle("Location", size(large) margin(medium)) xlab(1(1)4, valuelabel labsize(medium)) xsize(6) /*
				*/ legend(off) lcolor(black black black) lpattern(solid dash dash) lwidth(thick thick thick)  /*
				*/ graphregion(lcolor(white) fcolor(white)) bgcolor(white) plotregion(lstyle(none) fcolor(none)) /*
				*/ graphregion(margin(2 10 2 12)) connect(1 1 1) mlabel(a1) msymbol(S none none) /*
				*/  mlabposition(5) mlabcolor(gs0)  mlabgap(2) mcolor(gs0)
				graph export "C:\Users\maxha\Box\Abandoned Wells and Building\JAERE\Final Submission Files\reg_ftprnt_spatial.jpg", width(4800) as(jpg) replace
		restore
		
		preserve
			
			gen sample= (minyear<1920 & minyear>1850)
			keep if sample==1
			gen plugged_pre_1920=1 if (date_plugg_year<1920 & date_plugg_year>1850) | (date_plugg_year<1850 & minyear<=1900)
			keep if plugged_pre_1920==1 | unplugged_treat==1
			tab unplugged_treat
			
			
			gen SqM_Per_100SqM_1920_2017=pre_SqM_Per_100SqM_1920+post_SqM_Per_100SqM_2017
			*gen pre_SqM_Per_100SqM_0_to_1920= pre_SqM_Per_100SqM_0-pre_SqM_Per_100SqM_1920
			gen pre_SqM_Per_100SqM_0_to_1920_2= pre_SqM_Per_100SqM_0_to_1920^2
			
			gen building_1920_2017=(SqM_Per_100SqM_1920_2017>0)
				replace building_1920_2017= (building_1920_2017)*(((50^2)*_pi)/(((100^2)*_pi)-((50^2)*_pi))) if $d_c$low_high2 ==1
				replace building_1920_2017= (building_1920_2017)*(((50^2)*_pi)/(((150^2)*_pi)-((100^2)*_pi))) if $d_c$low_high3 ==1
				replace building_1920_2017= (building_1920_2017)*(((50^2)*_pi)/(((200^2)*_pi)-((150^2)*_pi))) if $d_c$low_high4 ==1
				replace building_1920_2017= (building_1920_2017)*(((50^2)*_pi)/(((250^2)*_pi)-((200^2)*_pi))) if $d_c$low_high5 ==1
			*gen building_0_1920=(pre_SqM_Per_100SqM_0_to_1920>0)
				*replace building_0_1920= (building_0_1920)*(((50^2)*_pi)/(((100^2)*_pi)-((50^2)*_pi))) if $d_c$low_high2 ==1
				*replace building_0_1920= (building_0_1920)*(((50^2)*_pi)/(((150^2)*_pi)-((100^2)*_pi))) if $d_c$low_high3 ==1
				*replace building_0_1920= (building_0_1920)*(((50^2)*_pi)/(((200^2)*_pi)-((150^2)*_pi))) if $d_c$low_high4 ==1
				*replace building_0_1920= (building_0_1920)*(((50^2)*_pi)/(((250^2)*_pi)-((200^2)*_pi))) if $d_c$low_high5 ==1
			
			gen lt_spatial_build= building_1920_2017-building_0_1920
			
			regress  lt_spatial_build $interactions $binaries unplugged_treat, cluster(well_id) 
				estimates store win2

				matrix b=e(b)'
				matrix doughnuts=1\2\3\4
				matrix d=(vecdiag(e(V)))'
				matmap d se,map(sqrt(@))
				matrix ci_u=b+1.96*se
				matrix ci_l=b-1.96*se
				matrix r= b,ci_l,ci_u
				svmat r, names(a)
				svmat doughnuts, names(d)
				
				label define dlabel 1 "0 to 50m" 2 "50 to 100m" 3 "100 to 150m" 4 "150 to 200m" 5 "200 to 250m"
				label values d1 dlabel
				
				replace a1=. if d1==.
				replace a2=. if d1==.
				replace a3=. if d1==.

				format  %9.3f a1
				
				qui: scatter a1 a2 a3 d1, ytitle("Probability of Building", size(large) margin(medium)) ylab(-.1 (.05).05, nogrid format(%9.0gc) /*
				*/ labsize(large) angle(0)) yline(0,lstyle(foreground)) ysize(3.5) xtitle("Location", size(large) margin(medium)) xlab(1(1)4, valuelabel labsize(medium)) xsize(6) /*
				*/ legend(off) lcolor(black black black) lpattern(solid dash dash) lwidth(thick thick thick)  /*
				*/ graphregion(lcolor(white) fcolor(white)) bgcolor(white) plotregion(lstyle(none) fcolor(none)) /*
				*/ graphregion(margin(2 10 2 12)) connect(1 1 1) mlabel(a1) msymbol(S none none) /*
				*/  mlabposition(1) mlabcolor(gs0)  mlabgap(2) mcolor(gs0)
				graph export "C:\Users\maxha\Box\Abandoned Wells and Building\JAERE\Final Submission Files\reg_share_lt_spatial.jpg", width(4800) as(jpg) replace
		restore
		
		preserve
			
			gen sample= (minyear<1920 & minyear>1850)
			keep if sample==1
			gen plugged_pre_1920=1 if (date_plugg_year<1920 & date_plugg_year>1850) | (date_plugg_year<1850 & minyear<=1900)
			keep if plugged_pre_1920==1 | unplugged_treat==1
			tab unplugged_treat
			
			gen SqM_Per_100SqM_1920_2017=pre_SqM_Per_100SqM_1920+post_SqM_Per_100SqM_2017
			*gen pre_SqM_Per_100SqM_0_to_1920= pre_SqM_Per_100SqM_0-pre_SqM_Per_100SqM_1920
			gen pre_SqM_Per_100SqM_0_to_1920_2= pre_SqM_Per_100SqM_0_to_1920^2
			
			gen building_1920_2017=(SqM_Per_100SqM_1920_2017>0)
				replace building_1920_2017= (building_1920_2017)*(((50^2)*_pi)/(((100^2)*_pi)-((50^2)*_pi))) if $d_c$low_high2 ==1
				replace building_1920_2017= (building_1920_2017)*(((50^2)*_pi)/(((150^2)*_pi)-((100^2)*_pi))) if $d_c$low_high3 ==1
				replace building_1920_2017= (building_1920_2017)*(((50^2)*_pi)/(((200^2)*_pi)-((150^2)*_pi))) if $d_c$low_high4 ==1
				replace building_1920_2017= (building_1920_2017)*(((50^2)*_pi)/(((250^2)*_pi)-((200^2)*_pi))) if $d_c$low_high5 ==1
			*gen building_0_1920=(pre_SqM_Per_100SqM_0_to_1920>0)
				*replace building_0_1920= (building_0_1920)*(((50^2)*_pi)/(((100^2)*_pi)-((50^2)*_pi))) if $d_c$low_high2 ==1
				*replace building_0_1920= (building_0_1920)*(((50^2)*_pi)/(((150^2)*_pi)-((100^2)*_pi))) if $d_c$low_high3 ==1
				*replace building_0_1920= (building_0_1920)*(((50^2)*_pi)/(((200^2)*_pi)-((150^2)*_pi))) if $d_c$low_high4 ==1
				*replace building_0_1920= (building_0_1920)*(((50^2)*_pi)/(((250^2)*_pi)-((200^2)*_pi))) if $d_c$low_high5 ==1
			
			gen lt_spatial_sqm= SqM_Per_100SqM_1920_2017-pre_SqM_Per_100SqM_0_to_1920

						
			regress  lt_spatial_sqm $interactions $binaries unplugged_treat, cluster(well_id) 
				estimates store win2

				matrix b=e(b)'
				matrix doughnuts=1\2\3\4
				matrix d=(vecdiag(e(V)))'
				matmap d se,map(sqrt(@))
				matrix ci_u=b+1.96*se
				matrix ci_l=b-1.96*se
				matrix r= b,ci_l,ci_u
				svmat r, names(a)
				svmat doughnuts, names(d)
				
				label define dlabel 1 "0 to 50m" 2 "50 to 100m" 3 "100 to 150m" 4 "150 to 200m" 5 "200 to 250m"
				label values d1 dlabel
				
				replace a1=. if d1==.
				replace a2=. if d1==.
				replace a3=. if d1==.

				format  %9.2f a1
				
				qui: scatter a1 a2 a3 d1, ytitle("Building Footprint (SqM Per 100 SqM)", size(large) margin(medium)) ylab(-3.5 (.5).5, nogrid format(%9.0gc) /*
				*/ labsize(large) angle(0)) yline(0,lstyle(foreground)) ysize(3.5) xtitle("Location", size(large) margin(medium)) xlab(1(1)4, valuelabel labsize(medium)) xsize(6) /*
				*/ legend(off) lcolor(black black black) lpattern(solid dash dash) lwidth(thick thick thick)  /*
				*/ graphregion(lcolor(white) fcolor(white)) bgcolor(white) plotregion(lstyle(none) fcolor(none)) /*
				*/ graphregion(margin(2 10 2 12)) connect(1 1 1) mlabel(a1) msymbol(S none none) /*
				*/  mlabposition(1) mlabcolor(gs0)  mlabgap(2) mcolor(gs0)
				graph export "C:\Users\maxha\Box\Abandoned Wells and Building\JAERE\Final Submission Files\reg_ftprnt_lt_spatial.jpg", width(4800) as(jpg) replace
		restore
}

*******************************************
*******************************************
*		SORTING ANALYSIS AND 			  *
*			ASSESSMENT DATA				  *	
*			   REGRESSIONS				  *
*******************************************
*******************************************	
	{
	clear
	clear matrix
	clear mata
	set maxvar 10000
	set more off
	*cd "C:\Users\mrh105\Box\EDWIN_DEP_Well_Data\Washington_County_Project\Forgone Value Calculations and Shifting Test" /*PC*/
	cd "C:\Users\maxha\Box\EDWIN_DEP_Well_Data\Washington_County_Project\Final Code\Forgone Value Calculations and Shifting Test" /*Home PC*/
	use Forgone_Value_Estimation.dta
	
	******************************
		***SORTING ANALYSIS***
	******************************

		gen distinct=1
		
		collapse (sum) TOTALVAL totalacres commercial_ACRES main_ACRES main_FootPrint_Area_SqM out_FootPrint_Area_SqM ///
		Count_Plug Count_UP distinct ///
		(max) central_ac HEAT_central , by(OWN1 ZIP1)
	
		gen up_only=(Count_UP>0 & Count_Plug==0)
		gen p_only=(Count_Plug>0 & Count_UP==0)
		drop if (up_only==0 & p_only==0)
		drop if (up_only==1 & p_only==1)
		
		gen own_plug=(p_only==1)
		
		gen acre_0_10=(totalacres>0 & totalacres<=10)
		gen acre_grtr10_25=(totalacres>10 & totalacres<=25)
		gen acre_grtr25_50=(totalacres>25 & totalacres<=50)
		gen acre_50_plus=(totalacres>50)
		
		ttest distinct, by(own_plug) unequal
		matrix a1= r(mu_1)
		matrix a2= r(mu_2)
		matrix a4= r(p)
		stddiff distinct, by(own_plug)
		matrix a3= r(stddiff)
		matrix d1= a1,a2,a3,a4
		matrix rownames d1 = "Distinct Properties"
		
		ttest totalacres, by(own_plug) unequal
		matrix a1= r(mu_1)
		matrix a2= r(mu_2)
		matrix a4= r(p)
		stddiff totalacres, by(own_plug)
		matrix a3= r(stddiff)
		matrix c1= a1,a2,a3,a4
		matrix rownames c1 = "Total Acres"
		
		ttest acre_0_10, by(own_plug) unequal
		matrix a1= r(mu_1)
		matrix a2= r(mu_2)
		matrix a4= r(p)
		stddiff acre_0_10, by(own_plug)
		matrix a3= r(stddiff)
		matrix c2= a1,a2,a3,a4
		matrix rownames c2 = "\emph{         Zero to 10 Acres}"
		
		ttest acre_grtr10_25, by(own_plug) unequal
		matrix a1= r(mu_1)
		matrix a2= r(mu_2)
		matrix a4= r(p)
		stddiff acre_grtr10_25, by(own_plug)
		matrix a3= r(stddiff)
		matrix c3= a1,a2,a3,a4
		matrix rownames c3 = "\emph{         10 to 25 Acres}"
		
		ttest acre_grtr25_50, by(own_plug) unequal
		matrix a1= r(mu_1)
		matrix a2= r(mu_2)
		matrix a4= r(p)
		stddiff acre_grtr25_50, by(own_plug)
		matrix a3= r(stddiff)
		matrix c4= a1,a2,a3,a4
		matrix rownames c4 = "\emph{         25 to 50 Acres}"
		
		ttest acre_50_plus, by(own_plug) unequal
		matrix a1= r(mu_1)
		matrix a2= r(mu_2)
		matrix a4= r(p)
		stddiff acre_50_plus, by(own_plug)
		matrix a3= r(stddiff)
		matrix c5= a1,a2,a3,a4
		matrix rownames c5 = "\emph{         50 Plus Acres}"
		
		ttest commercial_ACRES, by(own_plug) unequal
		matrix a1= r(mu_1)
		matrix a2= r(mu_2)
		matrix a4= r(p)
		stddiff commercial_ACRES, by(own_plug)
		matrix a3= r(stddiff)
		matrix c6= a1,a2,a3,a4
		matrix rownames c6 = "Commercial Acres"

		***DROP COMMERCIAL/UNASSESSED PARCELS***
		
		clear
		use Forgone_Value_Estimation.dta
		drop if TOTALVAL==0
		drop if commercial_parcel==1
		gen distinct=1
		
		/*DROP WASHINGTON AND CANONSBURG***
		drop if MUNI==61 /*WASHINGTON*/
		drop if MUNI==9 /*CANONSBURG*/ */
		
		collapse (sum) SFLA TOTALVAL totalacres total_FootPrint_Area_SqM main_FootPrint_Area_SqM commercial_FootPrint_Area_SqM out_FootPrint_Area_SqM ///
		Count_Plug Count_UP distinct ///
		(max) central_ac HEAT_central , by(OWN1 ZIP1)
	
		gen up_only=(Count_UP>0 & Count_Plug==0)
		gen p_only=(Count_Plug>0 & Count_UP==0)
		drop if (up_only==0 & p_only==0)
		drop if (up_only==1 & p_only==1)
		
		gen own_plug=(p_only==1)
		
		gen val_0_100=(TOTALVAL>0 & TOTALVAL<=100000)
		gen val_grtr100_250=(TOTALVAL>100000 & TOTALVAL<=250000)
		gen val_grtr250_500=(TOTALVAL>250000 & TOTALVAL<=500000)
		gen val_500_plus=(TOTALVAL>500000)
		
		ttest TOTALVAL, by(own_plug) unequal
		matrix a1= r(mu_1)
		matrix a2= r(mu_2)
		matrix a4= r(p)
		stddiff TOTALVAL, by(own_plug)
		matrix a3= r(stddiff)
		matrix r3= a1,a2,a3,a4
		matrix rownames r3 = "Assessed Res. Property (Dollars)"
		   
		ttest val_0_100, by(own_plug) unequal
		matrix a1= r(mu_1)
		matrix a2= r(mu_2)
		matrix a4= r(p)
		stddiff val_0_100, by(own_plug)
		matrix a3= r(stddiff)
		matrix r4= a1,a2,a3,a4
		matrix rownames r4 = "\emph{         Zero to 100K}"
	
		ttest val_grtr100_250, by(own_plug) unequal
		matrix a1= r(mu_1)
		matrix a2= r(mu_2)
		matrix a4= r(p)
		stddiff val_grtr100_250, by(own_plug)
		matrix a3= r(stddiff)
		matrix r5= a1,a2,a3,a4
		matrix rownames r5 = "\emph{         100K to 250K}"
		
		ttest val_grtr250_500, by(own_plug) unequal
		matrix a1= r(mu_1)
		matrix a2= r(mu_2)
		matrix a4= r(p)
		stddiff val_grtr250_500, by(own_plug)
		matrix a3= r(stddiff)
		matrix r6= a1,a2,a3,a4
		matrix rownames r6 = "\emph{         250K to 500K}"
		
		ttest val_500_plus, by(own_plug) unequal
		matrix a1= r(mu_1)
		matrix a2= r(mu_2)
		matrix a4= r(p)
		stddiff val_500_plus, by(own_plug)
		matrix a3= r(stddiff)
		matrix r7= a1,a2,a3,a4
		matrix rownames r7 = "\emph{         500K Plus}"
		
		ttest totalacres, by(own_plug) unequal
		matrix a1= r(mu_1)
		matrix a2= r(mu_2)
		matrix a4= r(p)
		stddiff totalacres, by(own_plug)
		matrix a3= r(stddiff)
		matrix r8= a1,a2,a3,a4
		matrix rownames r8 = "Residential Acres"
		
		ttest main_FootPrint_Area_SqM, by(own_plug) unequal
		matrix a1= r(mu_1)
		matrix a2= r(mu_2)
		matrix a4= r(p)
		stddiff main_FootPrint_Area_SqM, by(own_plug)
		matrix a3= r(stddiff)
		matrix r1= a1,a2,a3,a4
		matrix rownames r1 = "Residential Footprint (SqM)"
		
		ttest out_FootPrint_Area_SqM, by(own_plug) unequal
		matrix a1= r(mu_1)
		matrix a2= r(mu_2)
		matrix a4= r(p)
		stddiff out_FootPrint_Area_SqM, by(own_plug)
		matrix a3= r(stddiff)
		matrix r2= a1,a2,a3,a4
		matrix rownames r2 = "Out Footprint (SqM)"
		
		ttest central_ac, by(own_plug) unequal
		matrix a1= r(mu_1)
		matrix a2= r(mu_2)
		matrix a4= r(p)
		stddiff central_ac, by(own_plug)
		matrix a3= r(stddiff)
		matrix r9= a1,a2,a3,a4
		matrix rownames r9 = "\emph{Central AC}"
			
		ttest HEAT_central, by(own_plug) unequal
		matrix a1= r(mu_1)
		matrix a2= r(mu_2)
		matrix a4= r(p)
		stddiff HEAT_central, by(own_plug)
		matrix a3= r(stddiff)
		matrix r10= a1,a2,a3,a4
		matrix rownames r10 = "\emph{Central Heat}"
		
		matrix t1= r3\r4\r5\r6\r7\c1\c2\c3\c4\c5\r8\c6\d1\r1\r2\r9\r10
		matrix colnames t1 = "Owners Unplugged (N=890)" "Owners Plugged (N=1244)" "Standardized Mean Difference" "P-Value"

		esttab matrix(t1, fmt(%12.2fc)) using "C:\Users\maxha\Box\Abandoned Wells and Building\JAERE\Final Submission Files\sorting_balance.tex",  nomtitles replace	
		
		sum Count_Plug
		return list
		sum Count_UP
		return list

	
}
*******************************************
*******************************************
*			TRANSACTION DATA			  *
*			   REGRESSIONS				  *
*			   							  *
*******************************************
*******************************************
	{
	clear
	*cd "C:\Users\mrh105\Box\EDWIN_DEP_Well_Data\Washington_County_Project\Forgone Value Calculations and Shifting Test" /*PC*/
	cd "C:\Users\maxha\Box\EDWIN_DEP_Well_Data\Washington_County_Project\Final Code\Forgone Value Calculations and Shifting Test" /*Home PC*/
	use Forgone_Value_Estimation_Transactions.dta
	
	***LOG VARIABLES***
		gen ln_sale_price= ln(Sale_price)
		gen lntotalacres=ln(totalacres)
		gen lntotal_FootPrint_Area_SqM= ln(total_FootPrint_Area_SqM)

	***SQUARED VARIABLES***
		gen totalacres_sqd= totalacres^2
		gen main_FootPrint_Area_SqM_sqd= main_FootPrint_Area_SqM^2
		gen out_FootPrint_Area_SqM_sqd= out_FootPrint_Area_SqM^2
	
	***INTERACTION TERM***
		*gen post_1970_out=(outYRBLT>1970 & outYRBLT!=.)
		*gen out_SqM_interaction=(out_FootPrint_Area_SqM*post_1970_out)
		*gen post_1970_out=(weight_out_YRBLT>1970)
		*gen out_SqM_interaction=(out_FootPrint_Area_SqM*post_1970_out)
		
	***DUMMIES***
		gen out_dummy=(out_FootPrint_Area_SqM>0)
		gen main_dummy=(main_FootPrint_Area_SqM>0)
		
	/***MUNI LINEAR TRENDS***
		gen PIN3=PIN2/10
		drop PIN2
		rename PIN3 PIN2
		forvalues j=1/78{
		sort PIN2 sale_year
		by PIN2: gen trend_`j'=_n
		replace trend_`j'=0 if `j'~=PIN2
		} */
	
	***MUNI BY YEAR FES***
		tostring sale_year, gen (sale_year_str)
		egen pin2year=concat(PIN2 sale_year_str)
		destring(pin2year), replace
	
	***DROPS***
		drop if MUNI==61 /*WASHINGTON*/
		drop if MUNI==9 /*CANONSBURG*/
	
		drop if commercial_parcel==1 /*There are too few commercial parcels in the sample*/ 
		
		egen maxYRBLT= rowmax(outYRBLT mainYRBLT)
		drop if maxYRBLT>sale_year
		
	
	***TRANSACTION REGRESSIONS***

	xtset pin2year
		xi: xtreg Sale_price main_FootPrint_Area_SqM  out_FootPrint_Area_SqM totalacres public_h2o public_sewer public_gas if ((source=="WC" & sale_year<=2012) |(source=="Zillow" & sale_year>2012)) , fe vce(cluster pin2year)
			estimates store tran2
			
			matrix u= e(b)
			scalar mainsale= u[1,1]
			scalar outsale= u[1,2]
		
		esttab tran2 using "C:\Users\maxha\Box\Abandoned Wells and Building\JAERE\Final Submission Files\forgone_val_table.tex", label cells(b(nostar fmt(%9.0fc)) se(par fmt(%9.0fc))) ///
		coeflabels (main_FootPrint_Area_SqM "Square Meters of Residential Building" out_FootPrint_Area_SqM "Square Meters of Out Building" ///
		totalacres "Total Acres" public_h2o "Public Water Area" public_sewer "Public Sewer Area" public_gas "Public Gas Area") ///
		order(main_FootPrint_Area_SqM out_FootPrint_Area_SqM) ///
		nomtitles collabels("Sale Price") nodepvars nonumbers ///
		stats(r2_a N, fmt(2 %9.0fc) label(R-squared N)) ///
		replace
}
*******************************************
*******************************************
*			WELLS DATA MUNI				  *
*			   AND SCHOOL DISTRICT		  *
*			   							  *
*******************************************
*******************************************	
{
	clear
	*cd "C:\Users\mrh105\Box\EDWIN_DEP_Well_Data\Washington_County_Project\Forgone Value Calculations and Shifting Test" /*PC*/
	cd "C:\Users\maxha\Box\EDWIN_DEP_Well_Data\Washington_County_Project\Final Code\Forgone Value Calculations and Shifting Test" /*Home PC*/ 
	import dbase MuniWells_And_ShiftingData

	rename Muni MUNI
	rename Num_UP_Wel well_count
	
		preserve
			import dbase using "C:\Users\maxha\Box\EDWIN_DEP_Well_Data\Washington_County_Project\Final Code\Circle and Doughnut Tables\Plugged_wells_in_munis.dbf", clear
			replace NAME=upper(NAME)
			rename NAME MUNI
			rename mun_p_well well_count_p
			keep MUNI well_count_p
			gen I79= (MUNI=="CANONSBURG" | MUNI=="CECIL" | MUNI=="SOUTH STRABANE" | MUNI=="NORTH STRABANE" | MUNI=="WASHINGTON" | MUNI=="HOUSTON" )
			save merge_me.dta, replace
		restore
	
	merge 1:1 MUNI using merge_me.dta
	erase merge_me.dta
	
	preserve
		replace Shape_Area= Shape_Area*3.86102e-7
		gen up_wells_per_sqmile=well_count/Shape_Area
		gen pre_build_per_sqmile=Sqm_PreB/Shape_Area
		gen post_build_per_sqmile=Sqm_PostB/Shape_Area
		
		gen total_wells=well_count_p+well_count
		gen total_wells_per_sqmile=total_wells/Shape_Area

		regress post_build_per_sqmile up_wells_per_sqmile  pre_build_per_sqmile I79
		estimates store ee
		regress post_build_per_sqmile up_wells_per_sqmile  pre_build_per_sqmile I79 if well_count!=0
		estimates store ee2

		
		esttab ee ee2 using "C:\Users\maxha\Box\Abandoned Wells and Building\JAERE\Final Submission Files\shift_regg.tex", label cells(b(nostar fmt(%9.2fc)) se(par fmt(%9.2fc))) ///
		coeflabels (up_wells_per_sqmile "Unplugged Wells Per Square Mile" pre_build_per_sqmile "Pre-1970 Footprint Per Square Mile" I79 "Intersected by I-79") ///
		order(up_wells_per_sqmile pre_build_per_sqmile I79) ///
		nomtitles collabels(none) nodepvars ///
		stats(r2_a N, fmt(2 %9.0fc) label(R-squared N)) ///
		mgroups("All Munis" "With UP Wells", pattern(1 1) span prefix(\multicolumn{@span}{c}{) suffix(})) ///
		replace
	restore

	gen value_mainsale_SqM= mainsale
	gen value_outsale_SqM= outsale
	gen up_well_effect_main=(-0.844*(((50^2)*_pi)*(1/100)))* (-.21/-0.844)
	gen up_well_effect_out=(-0.844*(((50^2)*_pi)*(1/100)))* (-0.634/-0.844)

	gen forgone_val_sale_calc= (well_count*(value_mainsale_SqM*up_well_effect_main))+(well_count*(value_outsale_SqM*up_well_effect_out))

	egen sum_forgone_val_sale_calc = sum(forgone_val_sale_calc)
	
	format sum_forgone_val_sale_calc %20.2fc

	gen total_tax_base=17253760842.00 
	gen share_of_base_sale_calc= sum_forgone_val_sale_calc/total_tax_base

	gen county_millage=.00243
	drop _merge
	merge 1:m MUNI using TaxMillageWashingtonCounty.dta
	drop if _merge==2
	drop _merge

	gen lost_county_sale= county_millage*forgone_val_sale_calc
		summarize lost_county_sale
		display r(sum)
	gen lost_muni_sale= MUNICIPALMILLAGE*forgone_val_sale_calc
		summarize lost_muni_sale
		display r(sum)
	gen lost_school_sale= SCHOOLMILLAGE*forgone_val_sale_calc
		summarize lost_school_sale
		display r(sum)
		di r(sum)*50
	gen lost_total_sale=lost_county_sale+lost_muni_sale+lost_school_sale
		summarize lost_total_sale
		display r(sum)
	format lost_county_sale lost_muni_sale lost_school_sale lost_total_sale %20.2fc
	
	bysort SCHOOLDISTRICT: egen school_wells=total(well_count)
	gsort -school_wells
	sum lost_school_sale if SCHOOLDISTRICT=="MCGUFFEY", detail
	di r(sum)
	di r(sum)/1695 /*1,695 students enrolled according to 2016 COLGs*/
}
